2 * FM77AV/FM16β ALU [mb61vh010.cpp]
3 * of Fujitsu MB61VH010/011
5 * Author: K.Ohta <whatisthis.sowhat _at_ gmail.com>
8 * Mar 28, 2015 : Initial
12 #include "mb61vh010.h"
15 MB61VH010::MB61VH010(VM *parent_vm, EMU *parent_emu) : DEVICE(parent_vm, parent_emu)
21 MB61VH010::~MB61VH010()
25 uint8 MB61VH010::do_read(uint32 addr, uint32 bank)
29 if(((1 << bank) & multi_page) != 0) return 0xff;
31 if((addr & 0xffff) < 0x8000) {
32 raddr = (addr & 0x7fff) | (0x8000 * bank);
33 return target->read_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS);
36 raddr = (addr & 0x3fff) | (0x4000 * bank);
37 return target->read_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS);
42 uint8 MB61VH010::do_write(uint32 addr, uint32 bank, uint8 data)
47 if(((1 << bank) & multi_page) != 0) return 0xff;
48 if((command_reg & 0x40) != 0) { // Calculate before writing
49 readdata = do_read(addr, bank);
51 if((command_reg & 0x20) != 0) { // NAND
52 readdata = readdata & cmp_status_reg;
53 data = data & (~cmp_status_reg);
55 readdata = readdata & (~cmp_status_reg);
56 data = data & cmp_status_reg;
58 readdata = readdata | data;
63 if((addr & 0xffff) < 0x8000) {
64 //raddr = ((addr + (line_addr_offset.w.l << 1)) & 0x7fff) | (0x8000 * bank);
65 raddr = (addr & 0x7fff) | (0x8000 * bank);
66 target->write_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS, readdata);
69 //raddr = ((addr + (line_addr_offset.w.l << 1)) & 0x3fff) | (0x4000 * bank);
70 raddr = (addr & 0x3fff) | (0x4000 * bank);
71 target->write_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS, readdata);
77 uint8 MB61VH010::do_pset(uint32 addr)
80 uint32 raddr = addr; // Use banked ram.
83 int planes_b = planes;
84 if(planes_b >= 4) planes_b = 4;
85 for(i = 0; i < planes_b; i++) {
86 if((bank_disable_reg & (1 << i)) != 0) {
89 if((color_reg & (1 << i)) == 0) {
95 srcdata = do_read(addr, i);
96 bitmask = bitmask & (~mask_reg);
97 srcdata = srcdata & mask_reg;
98 srcdata = srcdata | bitmask;
99 do_write(addr, i, srcdata);
104 uint8 MB61VH010::do_blank(uint32 addr)
109 if(planes >= 4) planes = 4;
110 for(i = 0; i < planes; i++) {
111 if((bank_disable_reg & (1 << i)) != 0) {
114 srcdata = do_read(addr, i);
115 srcdata = srcdata & mask_reg;
116 do_write(addr, i, srcdata);
121 uint8 MB61VH010::do_or(uint32 addr)
127 if(planes >= 4) planes = 4;
128 for(i = 0; i < planes; i++) {
129 if((bank_disable_reg & (1 << i)) != 0) {
132 srcdata = do_read(addr, i);
133 if((color_reg & (1 << i)) == 0) {
134 bitmask = srcdata; // srcdata | 0x00
136 bitmask = 0xff; // srcdata | 0xff
138 bitmask = bitmask & ~mask_reg;
139 srcdata = (srcdata & mask_reg) | bitmask;
140 do_write(addr, i, srcdata);
145 uint8 MB61VH010::do_and(uint32 addr)
151 if(planes >= 4) planes = 4;
152 for(i = 0; i < planes; i++) {
153 if((bank_disable_reg & (1 << i)) != 0) {
156 srcdata = do_read(addr, i);
157 if((color_reg & (1 << i)) == 0) {
158 bitmask = 0x00; // srcdata & 0x00
160 bitmask = srcdata; // srcdata & 0xff;
162 bitmask = bitmask & ~mask_reg;
163 srcdata = (srcdata & mask_reg) | bitmask;
164 do_write(addr, i, srcdata);
169 uint8 MB61VH010::do_xor(uint32 addr)
175 if(planes >= 4) planes = 4;
176 for(i = 0; i < planes; i++) {
177 if((bank_disable_reg & (1 << i)) != 0) {
180 srcdata = do_read(addr, i);
181 if((color_reg & (1 << i)) == 0) {
182 bitmask = srcdata ^ 0x00;
184 bitmask = srcdata ^ 0xff;
186 bitmask = bitmask & ~mask_reg;
187 srcdata = (srcdata & mask_reg) | bitmask;
188 do_write(addr, i, srcdata);
193 uint8 MB61VH010::do_not(uint32 addr)
199 if(planes >= 4) planes = 4;
200 for(i = 0; i < planes; i++) {
201 if((bank_disable_reg & (1 << i)) != 0) {
204 srcdata = do_read(addr, i);
207 bitmask = bitmask & ~mask_reg;
208 srcdata = (srcdata & mask_reg) | bitmask;
209 do_write(addr, i, srcdata);
215 uint8 MB61VH010::do_tilepaint(uint32 addr)
220 //printf("Tilepaint CMD=%02x, ADDR=%04x Planes=%d, disable=%d, tile_reg=(%02x %02x %02x %02x)\n",
221 // command_reg, addr, planes, bank_disable_reg, tile_reg[0], tile_reg[1], tile_reg[2], tile_reg[3]);
222 if(planes > 4) planes = 4;
223 for(i = 0; i < planes; i++) {
224 if((bank_disable_reg & (1 << i)) != 0) {
227 srcdata = do_read(addr, i);
228 bitmask = tile_reg[i] & (~mask_reg);
229 srcdata = (srcdata & mask_reg) | bitmask;
230 do_write(addr, i, srcdata);
235 uint8 MB61VH010::do_compare(uint32 addr)
237 uint32 offset = 0x4000;
239 uint8 disables = ~bank_disable_reg;
240 //uint8 disables = bank_disable_reg;
243 uint8 cmp_reg_bak[8];
248 disables = disables & 0x07;
250 for(j = 0; j < 8; j++) {
251 if((cmp_color_data[j] & 0x80) == 0) {
252 cmp_reg_bak[k] = cmp_color_data[j] & disables;
256 cmp_status_reg = 0x00;
257 if(k <= 0) return 0xff;
259 b = do_read(addr, 0);
260 r = do_read(addr, 1);
261 g = do_read(addr, 2);
262 for(i = 0; i < 8; i++) {
264 tmpcol = (b & 0x80) >> 7;
265 tmpcol = tmpcol | ((r & 0x80) >> 6);
266 tmpcol = tmpcol | ((g & 0x80) >> 5);
267 //tmpcol |= ((t & 0x80) != 0) ? 8 : 0;
268 tmpcol = tmpcol & disables;
269 for(j = 0; j < k; j++) {
270 if(cmp_reg_bak[j] == tmpcol) {
271 tmp_stat = tmp_stat | 0x01;
280 cmp_status_reg = tmp_stat;
284 void MB61VH010::do_alucmds_dmyread(uint32 addr)
287 addr = addr & 0x3fff;
293 addr = addr & 0x7fff;
295 //printf("ALU DMYREAD: CMD %02x ADDR=%04x CMP[]=", command_reg, addr);
296 //for(i = 0; i < 8; i++) printf("[%02x]", cmp_color_data[i]);
297 if((command_reg & 0x80) == 0) {
301 //if(((command_reg & 0x40) != 0) && ((command_reg & 0x07) != 7)) do_compare(addr);
302 if((command_reg & 0x40) != 0) do_compare(addr);
303 switch(command_reg & 0x07) {
329 //printf("ALU DMYREAD ADDR=%04x, CMD=%02x CMP STATUS=%02x\n", addr, command_reg, cmp_status_reg);
332 uint8 MB61VH010::do_alucmds(uint32 addr)
335 addr = addr & 0x3fff;
341 addr = addr & 0x7fff;
343 //if(((command_reg & 0x40) != 0) && ((command_reg & 0x07) != 7)) do_compare(addr);
344 if((command_reg & 0x40) != 0) do_compare(addr);
345 switch(command_reg & 0x07) {
347 return do_pset(addr);
350 return do_blank(addr);
365 return do_tilepaint(addr);
368 return do_compare(addr);
374 void MB61VH010::do_line(void)
376 int x_begin = line_xbegin.w.l;
377 int x_end = line_xend.w.l;
378 int y_begin = line_ybegin.w.l;
379 int y_end = line_yend.w.l;
382 int ax = x_end - x_begin;
383 int ay = y_end - y_begin;
388 uint8 mask_bak = mask_reg;
390 uint8 vmask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
391 //printf("Line: (%d,%d) - (%d,%d) CMD=%02x\n", x_begin, y_begin, x_end, y_end, command_reg);
393 bool lastflag = false;
395 //is_400line = (target->read_signal(SIG_DISPLAY_MODE_IS_400LINE) != 0) ? true : false;
396 planes = target->read_signal(SIG_DISPLAY_PLANES) & 0x07;
397 screen_width = target->read_signal(SIG_DISPLAY_X_WIDTH) * 8;
398 screen_height = target->read_signal(SIG_DISPLAY_Y_HEIGHT);
400 //if((command_reg & 0x80) == 0) return;
401 oldaddr = 0xffffffff;
402 alu_addr = 0xffffffff;
405 line_style = line_pattern;
409 //mask_reg = 0xff & ~vmask[x_begin & 7];
411 if((line_style.b.h & 0x80) != 0) {
412 mask_reg &= ~vmask[cpx_t & 7];
414 tmp8a = (line_style.w.l & 0x8000) >> 15;
415 line_style.w.l = (line_style.w.l << 1) | tmp8a;
420 lastflag = put_dot(cpx_t, cpy_t);
425 for(; cpx_t <= x_end; cpx_t++) {
426 lastflag = put_dot(cpx_t, cpy_t);
427 if(lastflag) total_bytes++;
431 for(; cpx_t >= x_end; cpx_t--) {
432 lastflag = put_dot(cpx_t, cpy_t);
433 if(lastflag) total_bytes++;
439 } else if(xcount == 0) {
442 for(; cpy_t <= y_end; cpy_t++) {
443 lastflag = put_dot(cpx_t, cpy_t);
448 for(; cpy_t >= y_end; cpy_t--) {
449 lastflag = put_dot(cpx_t, cpy_t);
455 } else if(xcount > ycount) {
457 diff = ((ycount + 1) * 32768) / xcount;
458 for(; xcount >= 0; xcount-- ) {
459 lastflag = put_dot(cpx_t, cpy_t);
469 if(lastflag) total_bytes++;
476 } else if(xcount == ycount) {
478 for(; xcount >= 0; xcount-- ) {
479 lastflag = put_dot(cpx_t, cpy_t);
492 } else { // (abs(ax) < abs(ay)
494 diff = ((xcount + 1) * 32768) / ycount;
495 for(; ycount >= 0; ycount--) {
496 lastflag = put_dot(cpx_t, cpy_t);
516 if(!lastflag) total_bytes++;
517 do_alucmds(alu_addr);
519 if(total_bytes > 8) { // Over 0.5us
520 usec = (double)total_bytes / 16.0;
521 if(eventid_busy < 0) register_event(this, EVENT_MB61VH010_BUSY_OFF, usec, false, &eventid_busy) ;
525 //mask_reg = mask_bak;
526 //line_pattern = line_style;
529 bool MB61VH010::put_dot(int x, int y)
531 uint8 vmask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
535 if((x < 0) || (y < 0)) return flag;
536 if((x >= screen_width) || (y >= screen_height)) return flag;
537 if((command_reg & 0x80) == 0) return flag;
539 alu_addr = ((y * screen_width) >> 3) + (x >> 3);
540 alu_addr = alu_addr + (line_addr_offset.w.l << 1);
541 alu_addr = alu_addr & 0x7fff;
542 if(!is_400line) alu_addr = alu_addr & 0x3fff;
543 if(oldaddr == 0xffffffff) oldaddr = alu_addr;
545 if(oldaddr != alu_addr) {
553 if((line_style.b.h & 0x80) != 0) {
554 mask_reg &= ~vmask[x & 7];
556 tmp8a = (line_style.w.l & 0x8000) >> 15;
557 line_style.w.l = (line_style.w.l << 1) | tmp8a;
561 void MB61VH010::write_data8(uint32 id, uint32 data)
563 //printf("ALU: ADDR=%02x DATA=%02x\n", id, data);
564 if(id == ALU_CMDREG) {
568 //if((command_reg & 0x80) == 0) return;
570 case ALU_LOGICAL_COLOR:
573 case ALU_WRITE_MASKREG:
576 case ALU_BANK_DISABLE:
577 bank_disable_reg = data;
579 case ALU_TILEPAINT_B:
582 case ALU_TILEPAINT_R:
585 case ALU_TILEPAINT_G:
588 case ALU_TILEPAINT_L:
591 case ALU_OFFSET_REG_HIGH:
592 line_addr_offset.b.h = data;
594 case ALU_OFFSET_REG_LO:
595 line_addr_offset.b.l = data;
597 case ALU_LINEPATTERN_REG_HIGH:
598 line_pattern.b.h = data;
600 case ALU_LINEPATTERN_REG_LO:
601 line_pattern.b.l = data;
603 case ALU_LINEPOS_START_X_HIGH:
604 line_xbegin.b.h = data;
606 case ALU_LINEPOS_START_X_LOW:
607 line_xbegin.b.l = data;
609 case ALU_LINEPOS_START_Y_HIGH:
610 line_ybegin.b.h = data;
612 case ALU_LINEPOS_START_Y_LOW:
613 line_ybegin.b.l = data;
615 case ALU_LINEPOS_END_X_HIGH:
616 line_xend.b.h = data;
618 case ALU_LINEPOS_END_X_LOW:
619 line_xend.b.l = data;
621 case ALU_LINEPOS_END_Y_HIGH:
622 line_yend.b.h = data;
624 case ALU_LINEPOS_END_Y_LOW:
625 line_yend.b.l = data;
629 if((id >= (ALU_CMPDATA_REG + 0)) && (id < (ALU_CMPDATA_REG + 8))) {
630 cmp_color_data[id - ALU_CMPDATA_REG] = data;
631 } else if((id >= ALU_WRITE_PROXY) && (id < (ALU_WRITE_PROXY + 0x18000))) {
632 //is_400line = (target->read_signal(SIG_DISPLAY_MODE_IS_400LINE) != 0) ? true : false;
633 do_alucmds_dmyread(id - ALU_WRITE_PROXY);
639 uint32 MB61VH010::read_data8(uint32 id)
644 return (uint32)command_reg;
646 case ALU_LOGICAL_COLOR:
647 return (uint32)color_reg;
649 case ALU_WRITE_MASKREG:
650 return (uint32)mask_reg;
652 case ALU_CMP_STATUS_REG:
653 return (uint32)cmp_status_reg;
655 case ALU_BANK_DISABLE:
656 return (uint32)bank_disable_reg;
659 if((id >= ALU_WRITE_PROXY) && (id < (ALU_WRITE_PROXY + 0x18000))) {
661 raddr = (id - ALU_WRITE_PROXY) & 0xffff;
662 //is_400line = (target->read_signal(SIG_DISPLAY_MODE_IS_400LINE) != 0) ? true : false;
663 if(is_400line) raddr = raddr & 0x7fff;
664 dmydata = target->read_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS);
665 do_alucmds_dmyread(id - ALU_WRITE_PROXY);
666 dmydata = target->read_data8(raddr + DISPLAY_VRAM_DIRECT_ACCESS);
674 uint32 MB61VH010::read_signal(int id)
676 uint32 val = 0x00000000;
678 case SIG_ALU_BUSYSTAT:
679 if(busy_flag) val = 0xffffffff;
685 void MB61VH010::write_signal(int id, uint32 data, uint32 mask)
687 bool flag = ((data & mask) != 0);
689 case SIG_ALU_400LINE:
692 case SIG_ALU_MULTIPAGE:
693 multi_page = (data & mask) & 0x07;
698 void MB61VH010::event_callback(int event_id, int err)
701 case EVENT_MB61VH010_BUSY_ON:
703 if(eventid_busy >= 0) cancel_event(this, eventid_busy);
706 case EVENT_MB61VH010_BUSY_OFF:
713 void MB61VH010::initialize(void)
721 void MB61VH010::reset(void)
725 if(eventid_busy >= 0) cancel_event(this, eventid_busy);
728 command_reg = 0; // D410 (RW)
729 color_reg = 0; // D411 (RW)
730 mask_reg = 0; // D412 (RW)
731 cmp_status_reg = 0; // D413 (RO)
732 for(i = 0; i < 8; i++) cmp_color_data[i] = 0x80; // D413-D41A (WO)
733 bank_disable_reg = 0; // D41B (RW)
734 for(i = 0; i < 4; i++) tile_reg[i] = 0; // D41C-D41F (WO)
736 line_addr_offset.d = 0; // D420-D421 (WO)
737 line_pattern.d = 0; // D422-D423 (WO)
738 line_xbegin.d = 0; // D424-D425 (WO)
739 line_ybegin.d = 0; // D426-D427 (WO)
740 line_xend.d = 0; // D428-D429 (WO)
741 line_yend.d = 0; // D42A-D42B (WO)
743 oldaddr = 0xffffffff;
745 planes = target->read_signal(SIG_DISPLAY_PLANES) & 0x07;
746 if(planes >= 4) planes = 4;
747 //is_400line = (target->read_signal(SIG_DISPLAY_MODE_IS_400LINE) != 0) ? true : false;
749 screen_width = target->read_signal(SIG_DISPLAY_X_WIDTH) * 8;
750 screen_height = target->read_signal(SIG_DISPLAY_Y_HEIGHT);