OSDN Git Service

[VM][General] Merge upstream 2016-03-01. (Pahse 1).
[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         memory_bank = 0;
104         update_bank();
105         
106         memset(color_table, 7, sizeof(color_table));
107         char_color = 7;
108         back_color = mp1710_enb = 0;
109         
110         screen_mode = 0;
111         screen_reversed = false;
112         
113         drec_in = false;
114         
115         key_column = 0;
116         nmi_enb = break_pressed = false;
117         
118         sound_sample = 0;
119         sound_accum = 0;
120         sound_clock = sound_mix_clock = get_current_clock();
121 }
122
123 void MEMORY::write_data8(uint32_t addr, uint32_t data)
124 {
125         if((addr & 0xfe00) == 0xee00) {
126                 // EE00h - EFFFh
127                 switch(addr & 0xffff) {
128                 case 0xee40:
129                         screen_reversed = ((data & 0x80) != 0);
130                         break;
131                 case 0xee80:
132                         if(sound_sample != ((data >> 1) & 0x1f)) {
133                                 sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
134                                 sound_clock = get_current_clock();
135                                 sound_sample = (data >> 1) & 0x1f;
136                         }
137                         d_drec->write_signal(SIG_DATAREC_MIC, ~data, 0x01);
138                         break;
139                 case 0xeec0:
140                         key_column = data & 0x0f;
141                         nmi_enb = ((data & 0x80) != 0);
142                         event_frame(); // update keyboard
143                         break;
144                 case 0xefd0:
145                         // bit4: unknown (timer on/off)
146                         memory_bank = data;
147                         update_bank();
148                         break;
149                 case 0xefe0:
150                         screen_mode = data;
151                         break;
152                 }
153                 return;
154         }
155         if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
156                 // E800h - EDFFh
157                 switch(addr & 0xffff) {
158                 case 0xe800:
159                 case 0xe801:
160                 case 0xe802:
161                 case 0xe803:
162                         d_pia->write_io8(addr, data);
163                         break;
164                 case 0xe890:
165                         // bit0-3: fore color
166                         // bit4-7: back color
167                         char_color = data;
168                         break;
169                 case 0xe891:
170                         back_color = data;
171                         break;
172                 case 0xe892:
173                         mp1710_enb = data;
174                         break;
175                 }
176                 return;
177         }
178         if(addr >= 0x100 && addr < 0x400) {
179                 color_table[addr - 0x100] = char_color;
180         }
181         wbank[(addr >> 11) & 0x1f][addr & 0x7ff] = data;
182 }
183
184 uint32_t MEMORY::read_data8(uint32_t addr)
185 {
186         if((addr & 0xfe00) == 0xee00) {
187                 // EE00h - EFFFh
188                 switch(addr & 0xffff) {
189                 case 0xee00:
190                 case 0xee20:
191                         d_drec->write_signal(SIG_DATAREC_REMOTE, addr, 0x20);
192                         return 0x01;
193                 case 0xee80:
194                         return drec_in ? 0x80 : 0;
195                 case 0xeec0:
196                         return key_data;
197                 case 0xef00:
198                         // unknown (timer)
199                         break;
200                 case 0xef80:
201                         if(break_pressed) {
202                                 break_pressed = false;
203                                 return 0x80;
204                         }
205                         return 0x00;
206                 case 0xefd0:
207                         return memory_bank;
208                 }
209                 return 0xff;
210         }
211         if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
212                 // E800h - EDFFh
213                 switch(addr & 0xffff) {
214                 case 0xe800:
215                 case 0xe801:
216                 case 0xe802:
217                 case 0xe803:
218                         return d_pia->read_io8(addr);
219                 case 0xe890:
220                         return char_color;
221                 case 0xe891:
222                         return back_color;
223                 case 0xe892:
224                         return mp1710_enb;
225                 }
226                 return 0xff;
227         }
228         return rbank[(addr >> 11) & 0x1f][addr & 0x7ff];
229 }
230
231 void MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
232 {
233         if(id == SIG_MEMORY_DATAREC_EAR) {
234                 drec_in = ((data & mask) != 0);
235         }
236 }
237
238 void MEMORY::event_frame()
239 {
240         key_data = 0xff;
241         if(key_column < 13) {
242                 if(key_stat[key_table[key_column][0]]) key_data &= ~0x01;
243                 if(key_stat[key_table[key_column][1]]) key_data &= ~0x02;
244                 if(key_stat[key_table[key_column][2]]) key_data &= ~0x04;
245                 if(key_stat[key_table[key_column][3]]) key_data &= ~0x08;
246         }
247         // 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"
248         if(key_stat[0xa2]) key_data &= ~0x10; // \89p\90\94     -> L-CTRL
249         if(key_stat[0xa0]) key_data &= ~0x20; // \89p\8bL\8d\86   -> L-SHIFT
250         if(key_stat[0xa1]) key_data &= ~0x40; // \83J\83i\8bL\8d\86 -> R-SHIFT
251         if(key_stat[0xa3]) key_data &= ~0x80; // \83J\83i     -> R-CTRL
252 }
253
254 void MEMORY::key_down(int code)
255 {
256         // pause -> break
257         if(code == 0x13) {
258                 if(nmi_enb) {
259                         d_cpu->write_signal(SIG_CPU_NMI, 1, 1);
260                 }
261                 break_pressed = true;
262         }
263 }
264
265 void MEMORY::update_bank()
266 {
267         if(memory_bank & 1) {
268                 SET_BANK(0xb000, 0xdfff, ram + 0xb000, ram + 0xb000);
269         } else {
270                 SET_BANK(0xb000, 0xdfff, wdmy, basic);
271         }
272         if(memory_bank & 2) {
273                 SET_BANK(0xe000, 0xefff, ram + 0xe000, ram + 0xe000);
274         } else {
275                 SET_BANK(0xe000, 0xe7ff, wdmy, printer);
276                 SET_BANK(0xe800, 0xefff, wdmy, rdmy);   // memory mapped i/o
277         }
278         if(memory_bank & 4) {
279                 SET_BANK(0xf000, 0xffff, ram + 0xf000, ram + 0xf000);
280         } else {
281                 SET_BANK(0xf000, 0xffff, wdmy, monitor);
282         }
283 }
284
285 void MEMORY::mix(int32_t* buffer, int cnt)
286 {
287         int32_t volume = 0;
288         if(get_passed_clock(sound_mix_clock) != 0) {
289                 sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
290                 volume = (int32_t)(SOUND_VOLUME * sound_accum / (31.0 * get_passed_usec(sound_mix_clock)));
291         }
292         sound_accum = 0;
293         sound_clock = sound_mix_clock = get_current_clock();
294         
295         for(int i = 0; i < cnt; i++) {
296                 *buffer++ = apply_volume(volume, volume_l); // L
297                 *buffer++ = apply_volume(volume, volume_r); // R
298         }
299 }
300
301 void MEMORY::set_volume(int ch, int decibel_l, int decibel_r)
302 {
303         volume_l = decibel_to_volume(decibel_l);
304         volume_r = decibel_to_volume(decibel_r);
305 }
306
307 void MEMORY::draw_screen()
308 {
309         if(!(screen_mode & 0x80)) {
310                 // text
311                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
312                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
313                 int taddr = 0x100;
314                 
315                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
316                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
317                                 if(mp1710_enb & 1) {
318                                         uint8_t color = color_table[taddr];
319                                         if(screen_reversed) {
320                                                 color = (color >> 4) | (color << 4);
321                                         }
322                                         fore = palette_pc[(color     ) & 7];
323                                         back = palette_pc[(color >> 4) & 7];
324                                 }
325                                 int code = ram[taddr] << 3;
326                                 for(int l = 0; l < 8; l++) {
327                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
328                                         uint8_t pat = font[code + l];
329                                         dest[0] = (pat & 0x80) ? fore : back;
330                                         dest[1] = (pat & 0x40) ? fore : back;
331                                         dest[2] = (pat & 0x20) ? fore : back;
332                                         dest[3] = (pat & 0x10) ? fore : back;
333                                         dest[4] = (pat & 0x08) ? fore : back;
334                                         dest[5] = (pat & 0x04) ? fore : back;
335                                         dest[6] = (pat & 0x02) ? fore : back;
336                                         dest[7] = (pat & 0x01) ? fore : back;
337                                 }
338                                 taddr++;
339                         }
340                 }
341         } else {
342                 // graph
343                 scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
344                 scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
345                 int taddr = 0x100;
346                 int gaddr = 0x900 + ((screen_mode & 0x0f) << 9);
347                 
348                 for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
349                         for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
350                                 if(mp1710_enb & 1) {
351                                         uint8_t color = color_table[taddr];
352                                         if(screen_reversed) {
353                                                 color = (color >> 4) | (color << 4);
354                                         }
355                                         fore = palette_pc[(color     ) & 7];
356                                         back = palette_pc[(color >> 4) & 7];
357                                 }
358                                 for(int l = 0, ll = 0; l < 8; l++, ll += 32) {
359                                         scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
360                                         uint8_t pat = ram[gaddr + ll];
361                                         dest[0] = (pat & 0x80) ? fore : back;
362                                         dest[1] = (pat & 0x40) ? fore : back;
363                                         dest[2] = (pat & 0x20) ? fore : back;
364                                         dest[3] = (pat & 0x10) ? fore : back;
365                                         dest[4] = (pat & 0x08) ? fore : back;
366                                         dest[5] = (pat & 0x04) ? fore : back;
367                                         dest[6] = (pat & 0x02) ? fore : back;
368                                         dest[7] = (pat & 0x01) ? fore : back;
369                                 }
370                                 taddr++;
371                                 gaddr++;
372                         }
373                         gaddr += 32 * 7;
374                 }
375         }
376 //      emu->screen_skip_line(false);
377 }
378
379 #define STATE_VERSION   1
380
381 void MEMORY::save_state(FILEIO* state_fio)
382 {
383         state_fio->FputUint32(STATE_VERSION);
384         state_fio->FputInt32(this_device_id);
385         
386         state_fio->Fwrite(ram, sizeof(ram), 1);
387         state_fio->FputUint8(memory_bank);
388         state_fio->Fwrite(color_table, sizeof(color_table), 1);
389         state_fio->FputUint8(char_color);
390         state_fio->FputUint8(back_color);
391         state_fio->FputUint8(mp1710_enb);
392         state_fio->FputUint8(screen_mode);
393         state_fio->FputBool(screen_reversed);
394         state_fio->FputBool(drec_in);
395         state_fio->FputUint8(key_column);
396         state_fio->FputUint8(key_data);
397         state_fio->FputBool(nmi_enb);
398         state_fio->FputBool(break_pressed);
399         state_fio->FputUint8(sound_sample);
400         state_fio->FputDouble(sound_accum);
401         state_fio->FputUint32(sound_clock);
402         state_fio->FputUint32(sound_mix_clock);
403 }
404
405 bool MEMORY::load_state(FILEIO* state_fio)
406 {
407         if(state_fio->FgetUint32() != STATE_VERSION) {
408                 return false;
409         }
410         if(state_fio->FgetInt32() != this_device_id) {
411                 return false;
412         }
413         state_fio->Fread(ram, sizeof(ram), 1);
414         memory_bank = state_fio->FgetUint8();
415         state_fio->Fread(color_table, sizeof(color_table), 1);
416         char_color = state_fio->FgetUint8();
417         back_color = state_fio->FgetUint8();
418         mp1710_enb = state_fio->FgetUint8();
419         screen_mode = state_fio->FgetUint8();
420         screen_reversed = state_fio->FgetBool();
421         drec_in = state_fio->FgetBool();
422         key_column = state_fio->FgetUint8();
423         key_data = state_fio->FgetUint8();
424         nmi_enb = state_fio->FgetBool();
425         break_pressed = state_fio->FgetBool();
426         sound_sample = state_fio->FgetUint8();
427         sound_accum = state_fio->FgetDouble();
428         sound_clock = state_fio->FgetUint32();
429         sound_mix_clock = state_fio->FgetUint32();
430         
431         // post process
432         update_bank();
433         return true;
434 }
435