OSDN Git Service

2166b52a0b44fcb9533e670e94c8e766e093adfa
[csp-qt/common_source_project-fm7.git] / source / src / vm / bmjr / memory.cpp
1 /*
2         HITACH BASIC Master Jr Emulator 'eBASICMasterJr'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.08.28-
6
7         [ memory bus ]
8 */
9
10 #include "memory.h"
11 #include "../datarec.h"
12
13 #define SET_BANK(s, e, w, r) { \
14         int sb = (s) >> 11, eb = (e) >> 11; \
15         for(int i = sb; i <= eb; i++) { \
16                 if((w) == wdmy) { \
17                         wbank[i] = wdmy; \
18                 } else { \
19                         wbank[i] = (w) + 0x800 * (i - sb); \
20                 } \
21                 if((r) == rdmy) { \
22                         rbank[i] = rdmy; \
23                 } else { \
24                         rbank[i] = (r) + 0x800 * (i - sb); \
25                 } \
26         } \
27 }
28
29 #define SOUND_VOLUME    8000
30
31 static const int key_table[13][4] = {
32         {0x5a, 0x41, 0x51, 0x31},       //      'Z'     'A'     'Q'     '1'
33         {0x58, 0x53, 0x57, 0x32},       //      'X'     'S'     'W'     '2'
34         {0x43, 0x44, 0x45, 0x33},       //      'C'     'D'     'E'     '3'
35         {0x56, 0x46, 0x52, 0x34},       //      'V'     'F'     'R'     '4'
36         {0x42, 0x47, 0x54, 0x35},       //      'B'     'G'     'T'     '5'
37         {0x4e, 0x48, 0x59, 0x36},       //      'N'     'H'     'Y'     '6'
38         {0x4d, 0x4a, 0x55, 0x37},       //      'M'     'J'     'U'     '7'
39         {0xbc, 0x4b, 0x49, 0x38},       //      ','     'K'     'I'     '8'
40         {0xbe, 0x4c, 0x4f, 0x39},       //      '.'     'L'     'O'     '9'
41         {0xbf, 0xbb, 0x50, 0x30},       //      '/'     ';'     'P'     '0'
42         {0xe2, 0xba, 0xc0, 0xbd},       //      '_'     ':'     '@'     '-'
43         {0x20, 0xdd, 0xdb, 0xde},       //      SPACE   ']'     '['     '^'
44         {0x00, 0x0d, 0x2e, 0xdc},       //              RETURN  DEL     '\'
45 };
46
47 void MEMORY::initialize()
48 {
49         // initialize memory
50         memset(ram, 0, sizeof(ram));
51         memset(basic, 0xff, sizeof(basic));
52         memset(printer, 0xff, sizeof(printer));
53         memset(monitor, 0xff, sizeof(monitor));
54         memset(rdmy, 0xff, sizeof(rdmy));
55         
56         // load rom images
57         FILEIO* fio = new FILEIO();
58         if(fio->Fopen(create_local_path(_T("BASIC.ROM")), FILEIO_READ_BINARY)) {
59                 fio->Fread(basic, sizeof(basic), 1);
60                 fio->Fclose();
61         } else if(fio->Fopen(create_local_path(_T("BAS.ROM")), FILEIO_READ_BINARY)) {
62                 fio->Fread(basic, sizeof(basic), 1);
63                 fio->Fclose();
64         }
65         if(fio->Fopen(create_local_path(_T("PRINTER.ROM")), FILEIO_READ_BINARY)) {
66                 fio->Fread(printer, sizeof(printer), 1);
67                 fio->Fclose();
68         } else if(fio->Fopen(create_local_path(_T("PRT.ROM")), FILEIO_READ_BINARY)) {
69                 fio->Fread(printer, sizeof(printer), 1);
70                 fio->Fclose();
71         }
72         if(fio->Fopen(create_local_path(_T("MONITOR.ROM")), FILEIO_READ_BINARY)) {
73                 fio->Fread(monitor, sizeof(monitor), 1);
74                 fio->Fclose();
75         } else if(fio->Fopen(create_local_path(_T("MON.ROM")), FILEIO_READ_BINARY)) {
76                 fio->Fread(monitor, sizeof(monitor), 1);
77                 fio->Fclose();
78         }
79         if(fio->Fopen(create_local_path(_T("FONT.ROM")), FILEIO_READ_BINARY)) {
80                 fio->Fread(font, sizeof(font), 1);
81                 fio->Fclose();
82         }
83         delete fio;
84         
85         SET_BANK(0x0000, 0xafff, ram,  ram);
86         memory_bank = 0;
87         update_bank();
88         
89         // initialize inputs
90         key_stat = emu->get_key_buffer();
91         
92         // initialize display
93         for(int i = 0; i < 8; i++) {
94                 palette_pc[i] = RGB_COLOR((i & 2) ? 0xff : 0, (i & 4) ? 0xff : 0, (i & 1) ? 0xff : 0);
95         }
96         
97         // register event
98         register_frame_event(this);
99 }
100
101 void MEMORY::reset()
102 {
103         touch_sound();
104         memory_bank = 0;
105         update_bank();
106         
107         memset(color_table, 7, sizeof(color_table));
108         char_color = 7;
109         back_color = mp1710_enb = 0;
110         
111         screen_mode = 0;
112         screen_reversed = false;
113         
114         drec_in = false;
115         
116         key_column = 0;
117         nmi_enb = break_pressed = false;
118         
119         sound_sample = 0;
120         sound_accum = 0;
121         sound_clock = sound_mix_clock = get_current_clock();
122 }
123
124 void MEMORY::write_data8(uint32_t addr, uint32_t data)
125 {
126         if((addr & 0xfe00) == 0xee00) {
127                 // EE00h - EFFFh
128                 switch(addr & 0xffff) {
129                 case 0xee40:
130                         screen_reversed = ((data & 0x80) != 0);
131                         break;
132                 case 0xee80:
133                         if(sound_sample != ((data >> 1) & 0x1f)) {
134                                 touch_sound();
135                                 sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
136                                 sound_clock = get_current_clock();
137                                 sound_sample = (data >> 1) & 0x1f;
138                         }
139                         d_drec->write_signal(SIG_DATAREC_MIC, ~data, 0x01);
140                         break;
141                 case 0xeec0:
142                         key_column = data & 0x0f;
143                         nmi_enb = ((data & 0x80) != 0);
144                         event_frame(); // update keyboard
145                         break;
146                 case 0xefd0:
147                         // bit4: unknown (timer on/off)
148                         memory_bank = data;
149                         update_bank();
150                         break;
151                 case 0xefe0:
152                         screen_mode = data;
153                         break;
154                 }
155                 return;
156         }
157         if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
158                 // E800h - EDFFh
159                 switch(addr & 0xffff) {
160                 case 0xe800:
161                 case 0xe801:
162                 case 0xe802:
163                 case 0xe803:
164                         d_pia->write_io8(addr, data);
165                         break;
166                 case 0xe890:
167                         // bit0-3: fore color
168                         // bit4-7: back color
169                         char_color = data;
170                         break;
171                 case 0xe891:
172                         back_color = data;
173                         break;
174                 case 0xe892:
175                         mp1710_enb = data;
176                         break;
177                 }
178                 return;
179         }
180         if(addr >= 0x100 && addr < 0x400) {
181                 color_table[addr - 0x100] = char_color;
182         }
183         wbank[(addr >> 11) & 0x1f][addr & 0x7ff] = data;
184 }
185
186 uint32_t MEMORY::read_data8(uint32_t addr)
187 {
188         if((addr & 0xfe00) == 0xee00) {
189                 // EE00h - EFFFh
190                 switch(addr & 0xffff) {
191                 case 0xee00:
192                 case 0xee20:
193                         d_drec->write_signal(SIG_DATAREC_REMOTE, addr, 0x20);
194 #if defined(USE_SOUND_FILES)
195                         if(addr & 0x20) {
196                                 d_drec->write_signal(SIG_SOUNDER_ADD + DATAREC_SNDFILE_RELAY_ON, 1, 1);
197                         } else {
198                                 d_drec->write_signal(SIG_SOUNDER_ADD + DATAREC_SNDFILE_RELAY_OFF, 1, 1);
199                         }
200 #endif
201                         return 0x01;
202                 case 0xee80:
203                         return drec_in ? 0x80 : 0;
204                 case 0xeec0:
205                         return key_data;
206                 case 0xef00:
207                         // unknown (timer)
208                         break;
209                 case 0xef80:
210                         if(break_pressed) {
211                                 break_pressed = false;
212                                 return 0x80;
213                         }
214                         return 0x00;
215                 case 0xefd0:
216                         return memory_bank;
217                 }
218                 return 0xff;
219         }
220         if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
221                 // E800h - EDFFh
222                 switch(addr & 0xffff) {
223                 case 0xe800:
224                 case 0xe801:
225                 case 0xe802:
226                 case 0xe803:
227                         return d_pia->read_io8(addr);
228                 case 0xe890:
229                         return char_color;
230                 case 0xe891:
231                         return back_color;
232                 case 0xe892:
233                         return mp1710_enb;
234                 }
235                 return 0xff;
236         }
237         return rbank[(addr >> 11) & 0x1f][addr & 0x7ff];
238 }
239
240 void MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
241 {
242         if(id == SIG_MEMORY_DATAREC_EAR) {
243                 drec_in = ((data & mask) != 0);
244         }
245 }
246
247 void MEMORY::event_frame()
248 {
249         key_data = 0xff;
250         if(key_column < 13) {
251                 if(key_stat[key_table[key_column][0]]) key_data &= ~0x01;
252                 if(key_stat[key_table[key_column][1]]) key_data &= ~0x02;
253                 if(key_stat[key_table[key_column][2]]) key_data &= ~0x04;
254                 if(key_stat[key_table[key_column][3]]) key_data &= ~0x08;
255         }
256
257 #if defined(_USE_QT)
258         // If same as bm2, not effect below keys at Qt version.
259         if(key_stat[VK_LCONTROL]) key_data &= ~0x10; // \89p\90\94     -> LCTRL
260         if(key_stat[VK_LSHIFT]) key_data &= ~0x20; // \89p\8bL\8d\86   -> L-SHIFT
261         if(key_stat[VK_RWIN]) key_data &= ~0x40; // \83J\83i\8bL\8d\86 -> R-Win
262         if(key_stat[VK_KANA]) key_data &= ~0x80; // \83J\83i     -> \83J\83^\83J\83i\82Ð\82ç\82ª\82È
263 #else
264         // this is same as "\93ú\97§\83x\81[\83V\83b\83N\83}\83X\83^\81[Jr.(MB-6885)\83G\83~\83\85\83\8c\81[\83^ bm2"
265         if(key_stat[0xa2]) key_data &= ~0x10; // \89p\90\94     -> L-CTRL
266         if(key_stat[0xa0]) key_data &= ~0x20; // \89p\8bL\8d\86   -> L-SHIFT
267         if(key_stat[0xa1]) key_data &= ~0x40; // \83J\83i\8bL\8d\86 -> R-SHIFT
268         if(key_stat[0xa3]) key_data &= ~0x80; // \83J\83i     -> R-CTRL
269 #endif  
270 }
271
272 void MEMORY::key_down(int code)
273 {
274         // pause -> break
275         if(code == 0x13) {
276                 if(nmi_enb) {
277                         d_cpu->write_signal(SIG_CPU_NMI, 1, 1);
278                 }
279                 break_pressed = true;
280         }
281 }
282
283 void MEMORY::update_bank()
284 {
285         if(memory_bank & 1) {
286                 SET_BANK(0xb000, 0xdfff, ram + 0xb000, ram + 0xb000);
287         } else {
288                 SET_BANK(0xb000, 0xdfff, wdmy, basic);
289         }
290         if(memory_bank & 2) {
291                 SET_BANK(0xe000, 0xefff, ram + 0xe000, ram + 0xe000);
292         } else {
293                 SET_BANK(0xe000, 0xe7ff, wdmy, printer);
294                 SET_BANK(0xe800, 0xefff, wdmy, rdmy);   // memory mapped i/o
295         }
296         if(memory_bank & 4) {
297                 SET_BANK(0xf000, 0xffff, ram + 0xf000, ram + 0xf000);
298         } else {
299                 SET_BANK(0xf000, 0xffff, wdmy, monitor);
300         }
301 }
302
303 void MEMORY::mix(int32_t* buffer, int cnt)
304 {
305         int32_t volume = 0;
306         if(get_passed_clock(sound_mix_clock) != 0) {
307                 sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
308                 volume = (int32_t)(SOUND_VOLUME * sound_accum / (31.0 * get_passed_usec(sound_mix_clock)));
309         }
310         sound_accum = 0;
311         sound_clock = sound_mix_clock = get_current_clock();
312         
313         for(int i = 0; i < cnt; i++) {
314                 *buffer++ = apply_volume(volume, volume_l); // L
315                 *buffer++ = apply_volume(volume, volume_r); // R
316         }
317 }
318
319 void MEMORY::set_volume(int ch, int decibel_l, int decibel_r)
320 {
321         volume_l = decibel_to_volume(decibel_l);
322         volume_r = decibel_to_volume(decibel_r);
323 }
324
325 void MEMORY::draw_screen()
326 {
327         if(!(screen_mode & 0x80)) {
328                 // text
329                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
330                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
331                 int taddr = 0x100;
332                 
333                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
334                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
335                                 if(mp1710_enb & 1) {
336                                         uint8_t color = color_table[taddr];
337                                         if(screen_reversed) {
338                                                 color = (color >> 4) | (color << 4);
339                                         }
340                                         fore = palette_pc[(color     ) & 7];
341                                         back = palette_pc[(color >> 4) & 7];
342                                 }
343                                 int code = ram[taddr] << 3;
344                                 for(int l = 0; l < 8; l++) {
345                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
346                                         uint8_t pat = font[code + l];
347                                         dest[0] = (pat & 0x80) ? fore : back;
348                                         dest[1] = (pat & 0x40) ? fore : back;
349                                         dest[2] = (pat & 0x20) ? fore : back;
350                                         dest[3] = (pat & 0x10) ? fore : back;
351                                         dest[4] = (pat & 0x08) ? fore : back;
352                                         dest[5] = (pat & 0x04) ? fore : back;
353                                         dest[6] = (pat & 0x02) ? fore : back;
354                                         dest[7] = (pat & 0x01) ? fore : back;
355                                 }
356                                 taddr++;
357                         }
358                 }
359         } else {
360                 // graph
361                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
362                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
363                 int taddr = 0x100;
364                 int gaddr = 0x900 + ((screen_mode & 0x0f) << 9);
365                 
366                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
367                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
368                                 if(mp1710_enb & 1) {
369                                         uint8_t color = color_table[taddr];
370                                         if(screen_reversed) {
371                                                 color = (color >> 4) | (color << 4);
372                                         }
373                                         fore = palette_pc[(color     ) & 7];
374                                         back = palette_pc[(color >> 4) & 7];
375                                 }
376                                 for(int l = 0, ll = 0; l < 8; l++, ll += 32) {
377                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
378                                         uint8_t pat = ram[gaddr + ll];
379                                         dest[0] = (pat & 0x80) ? fore : back;
380                                         dest[1] = (pat & 0x40) ? fore : back;
381                                         dest[2] = (pat & 0x20) ? fore : back;
382                                         dest[3] = (pat & 0x10) ? fore : back;
383                                         dest[4] = (pat & 0x08) ? fore : back;
384                                         dest[5] = (pat & 0x04) ? fore : back;
385                                         dest[6] = (pat & 0x02) ? fore : back;
386                                         dest[7] = (pat & 0x01) ? fore : back;
387                                 }
388                                 taddr++;
389                                 gaddr++;
390                         }
391                         gaddr += 32 * 7;
392                 }
393         }
394 //      emu->screen_skip_line(false);
395 }
396
397 #define STATE_VERSION   1
398
399 void MEMORY::save_state(FILEIO* state_fio)
400 {
401         state_fio->FputUint32(STATE_VERSION);
402         state_fio->FputInt32(this_device_id);
403         
404         state_fio->Fwrite(ram, sizeof(ram), 1);
405         state_fio->FputUint8(memory_bank);
406         state_fio->Fwrite(color_table, sizeof(color_table), 1);
407         state_fio->FputUint8(char_color);
408         state_fio->FputUint8(back_color);
409         state_fio->FputUint8(mp1710_enb);
410         state_fio->FputUint8(screen_mode);
411         state_fio->FputBool(screen_reversed);
412         state_fio->FputBool(drec_in);
413         state_fio->FputUint8(key_column);
414         state_fio->FputUint8(key_data);
415         state_fio->FputBool(nmi_enb);
416         state_fio->FputBool(break_pressed);
417         state_fio->FputUint8(sound_sample);
418         state_fio->FputDouble(sound_accum);
419         state_fio->FputUint32(sound_clock);
420         state_fio->FputUint32(sound_mix_clock);
421 }
422
423 bool MEMORY::load_state(FILEIO* state_fio)
424 {
425         if(state_fio->FgetUint32() != STATE_VERSION) {
426                 return false;
427         }
428         if(state_fio->FgetInt32() != this_device_id) {
429                 return false;
430         }
431         state_fio->Fread(ram, sizeof(ram), 1);
432         memory_bank = state_fio->FgetUint8();
433         state_fio->Fread(color_table, sizeof(color_table), 1);
434         char_color = state_fio->FgetUint8();
435         back_color = state_fio->FgetUint8();
436         mp1710_enb = state_fio->FgetUint8();
437         screen_mode = state_fio->FgetUint8();
438         screen_reversed = state_fio->FgetBool();
439         drec_in = state_fio->FgetBool();
440         key_column = state_fio->FgetUint8();
441         key_data = state_fio->FgetUint8();
442         nmi_enb = state_fio->FgetBool();
443         break_pressed = state_fio->FgetBool();
444         sound_sample = state_fio->FgetUint8();
445         sound_accum = state_fio->FgetDouble();
446         sound_clock = state_fio->FgetUint32();
447         sound_mix_clock = state_fio->FgetUint32();
448         
449         // post process
450         update_bank();
451         return true;
452 }
453