OSDN Git Service

[VM] MEMORY:: class within some VM will change Foo_MEMORY:: to reduce misundestanding...
[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 BMJR_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 BMJR_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_bit = 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 BMJR_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 - EFFFh
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 BMJR_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                         return 0x01;
195                 case 0xee80:
196                         return drec_bit ? 0x80 : 0;
197                 case 0xeec0:
198                         return key_data;
199                 case 0xef00:
200                         // unknown (timer)
201                         break;
202                 case 0xef80:
203                         if(break_pressed) {
204                                 break_pressed = false;
205                                 return 0x80;
206                         }
207                         return 0x00;
208                 case 0xefd0:
209                         return memory_bank;
210                 }
211                 return 0xff;
212         }
213         if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
214                 // E800h - EFFFh
215                 switch(addr & 0xffff) {
216                 case 0xe800:
217                 case 0xe801:
218                 case 0xe802:
219                 case 0xe803:
220                         return d_pia->read_io8(addr);
221                 case 0xe890:
222                         return char_color;
223                 case 0xe891:
224                         return back_color;
225                 case 0xe892:
226                         return mp1710_enb;
227                 }
228                 return 0xff;
229         }
230         return rbank[(addr >> 11) & 0x1f][addr & 0x7ff];
231 }
232
233 void BMJR_MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
234 {
235         if(id == SIG_MEMORY_DATAREC_EAR) {
236                 bool new_in = ((data & mask) != 0);
237                 if(!drec_in && new_in) {
238                         // L -> H
239                         drec_clock = get_current_clock();
240                 } else if(drec_in && !new_in) {
241                         // H -> L
242                         int usec = (int)get_passed_usec(drec_clock);
243                         if(usec > 417 - 42 && usec < 417 + 42) {
244                                 drec_bit = false;       // 1200Hz
245                         } else if(usec > 208 - 21 && usec < 208 + 21) {
246                                 drec_bit = true;        // 2400Hz
247                         }
248                 }
249                 drec_in = new_in;
250         }
251 }
252
253 void BMJR_MEMORY::event_frame()
254 {
255         key_data = 0xff;
256         if(key_column < 13) {
257                 if(key_stat[key_table[key_column][0]]) key_data &= ~0x01;
258                 if(key_stat[key_table[key_column][1]]) key_data &= ~0x02;
259                 if(key_stat[key_table[key_column][2]]) key_data &= ~0x04;
260                 if(key_stat[key_table[key_column][3]]) key_data &= ~0x08;
261         }
262
263 #if defined(_USE_QT)
264         // If same as bm2, not effect below keys at Qt version.
265         if(key_stat[VK_LCONTROL]) key_data &= ~0x10; // 英数     -> LCTRL
266         if(key_stat[VK_LSHIFT  ]) key_data &= ~0x20; // 英記号   -> L-SHIFT
267         if(key_stat[VK_RWIN    ]) key_data &= ~0x40; // カナ記号 -> R-Win
268         if(key_stat[VK_KANA    ]) key_data &= ~0x80; // カナ     -> カタカナひらがな
269 #else
270         // this is same as "日立ベーシックマスターJr.(MB-6885)エミュレータ bm2"
271         if(key_stat[0xa2]) key_data &= ~0x10; // 英数     -> L-CTRL
272         if(key_stat[0xa0]) key_data &= ~0x20; // 英記号   -> L-SHIFT
273         if(key_stat[0xa1]) key_data &= ~0x40; // カナ記号 -> R-SHIFT
274         if(key_stat[0xa3]) key_data &= ~0x80; // カナ     -> R-CTRL
275 #endif
276 }
277
278 void BMJR_MEMORY::key_down(int code)
279 {
280         // pause -> break
281         if(code == 0x13) {
282                 if(nmi_enb) {
283                         d_cpu->write_signal(SIG_CPU_NMI, 1, 1);
284                 }
285                 break_pressed = true;
286         }
287 }
288
289 void BMJR_MEMORY::update_bank()
290 {
291         if(memory_bank & 1) {
292                 SET_BANK(0xb000, 0xdfff, ram + 0xb000, ram + 0xb000);
293         } else {
294                 SET_BANK(0xb000, 0xdfff, wdmy, basic);
295         }
296         if(memory_bank & 2) {
297                 SET_BANK(0xe000, 0xefff, ram + 0xe000, ram + 0xe000);
298         } else {
299                 SET_BANK(0xe000, 0xe7ff, wdmy, printer);
300                 SET_BANK(0xe800, 0xefff, wdmy, rdmy);   // memory mapped i/o
301         }
302         if(memory_bank & 4) {
303                 SET_BANK(0xf000, 0xffff, ram + 0xf000, ram + 0xf000);
304         } else {
305                 SET_BANK(0xf000, 0xffff, wdmy, monitor);
306         }
307 }
308
309 void BMJR_MEMORY::mix(int32_t* buffer, int cnt)
310 {
311         int32_t volume = 0;
312         if(get_passed_clock(sound_mix_clock) != 0) {
313                 sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
314                 volume = (int32_t)(SOUND_VOLUME * sound_accum / (31.0 * get_passed_usec(sound_mix_clock)));
315         }
316         sound_accum = 0;
317         sound_clock = sound_mix_clock = get_current_clock();
318         
319         int32_t vol_l = apply_volume(volume, volume_l);
320         int32_t vol_r = apply_volume(volume, volume_r);
321         
322         for(int i = 0; i < cnt; i++) {
323                 *buffer++ += vol_l; // L
324                 *buffer++ += vol_r; // R
325         }
326 }
327
328 void BMJR_MEMORY::set_volume(int ch, int decibel_l, int decibel_r)
329 {
330         volume_l = decibel_to_volume(decibel_l);
331         volume_r = decibel_to_volume(decibel_r);
332 }
333
334 void BMJR_MEMORY::draw_screen()
335 {
336         if(!(screen_mode & 0x80)) {
337                 // text
338                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
339                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
340                 int taddr = 0x100;
341                 
342                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
343                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
344                                 if(mp1710_enb & 1) {
345                                         uint8_t color = color_table[taddr];
346                                         if(screen_reversed) {
347                                                 color = (color >> 4) | (color << 4);
348                                         }
349                                         fore = palette_pc[(color     ) & 7];
350                                         back = palette_pc[(color >> 4) & 7];
351                                 }
352                                 int code = ram[taddr] << 3;
353                                 for(int l = 0; l < 8; l++) {
354                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
355                                         uint8_t pat = font[code + l];
356                                         dest[0] = (pat & 0x80) ? fore : back;
357                                         dest[1] = (pat & 0x40) ? fore : back;
358                                         dest[2] = (pat & 0x20) ? fore : back;
359                                         dest[3] = (pat & 0x10) ? fore : back;
360                                         dest[4] = (pat & 0x08) ? fore : back;
361                                         dest[5] = (pat & 0x04) ? fore : back;
362                                         dest[6] = (pat & 0x02) ? fore : back;
363                                         dest[7] = (pat & 0x01) ? fore : back;
364                                 }
365                                 taddr++;
366                         }
367                 }
368         } else {
369                 // graph
370                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
371                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
372                 int taddr = 0x100;
373                 int gaddr = 0x900 + ((screen_mode & 0x0f) << 9);
374                 
375                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
376                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
377                                 if(mp1710_enb & 1) {
378                                         uint8_t color = color_table[taddr];
379                                         if(screen_reversed) {
380                                                 color = (color >> 4) | (color << 4);
381                                         }
382                                         fore = palette_pc[(color     ) & 7];
383                                         back = palette_pc[(color >> 4) & 7];
384                                 }
385                                 for(int l = 0, ll = 0; l < 8; l++, ll += 32) {
386                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
387                                         uint8_t pat = ram[gaddr + ll];
388                                         dest[0] = (pat & 0x80) ? fore : back;
389                                         dest[1] = (pat & 0x40) ? fore : back;
390                                         dest[2] = (pat & 0x20) ? fore : back;
391                                         dest[3] = (pat & 0x10) ? fore : back;
392                                         dest[4] = (pat & 0x08) ? fore : back;
393                                         dest[5] = (pat & 0x04) ? fore : back;
394                                         dest[6] = (pat & 0x02) ? fore : back;
395                                         dest[7] = (pat & 0x01) ? fore : back;
396                                 }
397                                 taddr++;
398                                 gaddr++;
399                         }
400                         gaddr += 32 * 7;
401                 }
402         }
403 //      emu->screen_skip_line(false);
404 }
405
406 #define STATE_VERSION   2
407
408 bool BMJR_MEMORY::process_state(FILEIO* state_fio, bool loading)
409 {
410         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
411                 return false;
412         }
413         if(!state_fio->StateCheckInt32(this_device_id)) {
414                 return false;
415         }
416         state_fio->StateBuffer(ram, sizeof(ram), 1);
417         state_fio->StateUint8(memory_bank);
418         state_fio->StateBuffer(color_table, sizeof(color_table), 1);
419         state_fio->StateUint8(char_color);
420         state_fio->StateUint8(back_color);
421         state_fio->StateUint8(mp1710_enb);
422         state_fio->StateUint8(screen_mode);
423         state_fio->StateBool(screen_reversed);
424         state_fio->StateBool(drec_bit);
425         state_fio->StateBool(drec_in);
426         state_fio->StateUint32(drec_clock);
427         state_fio->StateUint8(key_column);
428         state_fio->StateUint8(key_data);
429         state_fio->StateBool(nmi_enb);
430         state_fio->StateBool(break_pressed);
431         state_fio->StateUint8(sound_sample);
432         state_fio->StateDouble(sound_accum);
433         state_fio->StateUint32(sound_clock);
434         state_fio->StateUint32(sound_mix_clock);
435         
436         // post process
437         if(loading) {
438                 update_bank();
439         }
440         return true;
441 }