| 1 | /* |
| 2 | * TODO: |
| 3 | * - start in a state as if BIOS ran |
| 4 | * - remaining status flags (OVR/COL) |
| 5 | * - RAM support in mapper |
| 6 | * - region support |
| 7 | * - Pause button (NMI) |
| 8 | * - SN76496 DAC-like usage |
| 9 | * - H counter |
| 10 | */ |
| 11 | #include "pico_int.h" |
| 12 | #include "sound/sn76496.h" |
| 13 | |
| 14 | static unsigned char vdp_data_read(void) |
| 15 | { |
| 16 | struct PicoVideo *pv = &Pico.video; |
| 17 | unsigned char d; |
| 18 | |
| 19 | d = Pico.vramb[pv->addr]; |
| 20 | pv->addr = (pv->addr + 1) & 0x3fff; |
| 21 | pv->pending = 0; |
| 22 | return d; |
| 23 | } |
| 24 | |
| 25 | static unsigned char vdp_ctl_read(void) |
| 26 | { |
| 27 | unsigned char d = Pico.video.pending_ints << 7; |
| 28 | Pico.video.pending = 0; |
| 29 | Pico.video.pending_ints = 0; |
| 30 | |
| 31 | elprintf(EL_SR, "VDP sr: %02x", d); |
| 32 | return d; |
| 33 | } |
| 34 | |
| 35 | static void vdp_data_write(unsigned char d) |
| 36 | { |
| 37 | struct PicoVideo *pv = &Pico.video; |
| 38 | |
| 39 | if (pv->type == 3) { |
| 40 | Pico.cram[pv->addr & 0x1f] = d; |
| 41 | Pico.m.dirtyPal = 1; |
| 42 | } else { |
| 43 | Pico.vramb[pv->addr] = d; |
| 44 | } |
| 45 | pv->addr = (pv->addr + 1) & 0x3fff; |
| 46 | |
| 47 | pv->pending = 0; |
| 48 | } |
| 49 | |
| 50 | static void vdp_ctl_write(unsigned char d) |
| 51 | { |
| 52 | struct PicoVideo *pv = &Pico.video; |
| 53 | |
| 54 | if (pv->pending) { |
| 55 | if ((d >> 6) == 2) { |
| 56 | pv->reg[d & 0x0f] = pv->addr; |
| 57 | elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff); |
| 58 | } |
| 59 | pv->type = d >> 6; |
| 60 | pv->addr &= 0x00ff; |
| 61 | pv->addr |= (d & 0x3f) << 8; |
| 62 | } else { |
| 63 | pv->addr &= 0x3f00; |
| 64 | pv->addr |= d; |
| 65 | } |
| 66 | pv->pending ^= 1; |
| 67 | } |
| 68 | |
| 69 | static unsigned char z80_sms_in(unsigned short a) |
| 70 | { |
| 71 | unsigned char d = 0; |
| 72 | |
| 73 | elprintf(EL_IO, "z80 port %04x read", a); |
| 74 | a &= 0xc1; |
| 75 | switch (a) |
| 76 | { |
| 77 | case 0x00: |
| 78 | case 0x01: |
| 79 | d = 0xff; |
| 80 | break; |
| 81 | |
| 82 | case 0x40: /* V counter */ |
| 83 | d = Pico.video.v_counter; |
| 84 | elprintf(EL_HVCNT, "V counter read: %02x", d); |
| 85 | break; |
| 86 | |
| 87 | case 0x41: /* H counter */ |
| 88 | d = Pico.m.rotate++; |
| 89 | elprintf(EL_HVCNT, "H counter read: %02x", d); |
| 90 | break; |
| 91 | |
| 92 | case 0x80: |
| 93 | d = vdp_data_read(); |
| 94 | break; |
| 95 | |
| 96 | case 0x81: |
| 97 | d = vdp_ctl_read(); |
| 98 | break; |
| 99 | |
| 100 | case 0xc0: /* I/O port A and B */ |
| 101 | d = ~((PicoPad[0] & 0x3f) | (PicoPad[1] << 6)); |
| 102 | break; |
| 103 | |
| 104 | case 0xc1: /* I/O port B and miscellaneous */ |
| 105 | d = (Pico.sms_io_ctl & 0x80) | ((Pico.sms_io_ctl << 1) & 0x40) | 0x30; |
| 106 | d |= ~(PicoPad[1] >> 2) & 0x0f; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | elprintf(EL_IO, "ret = %02x", d); |
| 111 | return d; |
| 112 | } |
| 113 | |
| 114 | static void z80_sms_out(unsigned short a, unsigned char d) |
| 115 | { |
| 116 | elprintf(EL_IO, "z80 port %04x write %02x", a, d); |
| 117 | a &= 0xc1; |
| 118 | switch (a) |
| 119 | { |
| 120 | case 0x01: |
| 121 | Pico.sms_io_ctl = d; |
| 122 | break; |
| 123 | |
| 124 | case 0x40: |
| 125 | case 0x41: |
| 126 | if (PicoOpt & POPT_EN_PSG) |
| 127 | SN76496Write(d); |
| 128 | break; |
| 129 | |
| 130 | case 0x80: |
| 131 | vdp_data_write(d); |
| 132 | break; |
| 133 | |
| 134 | case 0x81: |
| 135 | vdp_ctl_write(d); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int bank_mask; |
| 141 | |
| 142 | static void write_bank(unsigned short a, unsigned char d) |
| 143 | { |
| 144 | elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc()); |
| 145 | switch (a & 0x0f) |
| 146 | { |
| 147 | case 0x0c: |
| 148 | elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d); |
| 149 | break; |
| 150 | case 0x0d: |
| 151 | if (d != 0) |
| 152 | elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d); |
| 153 | break; |
| 154 | case 0x0e: |
| 155 | d &= bank_mask; |
| 156 | z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0); |
| 157 | #ifdef _USE_CZ80 |
| 158 | Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (UINT32)Pico.rom + (d << 14)); |
| 159 | #endif |
| 160 | break; |
| 161 | case 0x0f: |
| 162 | d &= bank_mask; |
| 163 | z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0); |
| 164 | #ifdef _USE_CZ80 |
| 165 | Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (UINT32)Pico.rom + (d << 14)); |
| 166 | #endif |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void MEMH_FUNC xwrite(unsigned int a, unsigned char d) |
| 172 | { |
| 173 | elprintf(EL_IO, "z80 write [%04x] %02x", a, d); |
| 174 | if (a >= 0xc000) |
| 175 | Pico.zram[a & 0x1fff] = d; |
| 176 | if (a >= 0xfff0) |
| 177 | write_bank(a, d); |
| 178 | } |
| 179 | |
| 180 | void PicoResetMS(void) |
| 181 | { |
| 182 | z80_reset(); |
| 183 | PsndReset(); // pal must be known here |
| 184 | } |
| 185 | |
| 186 | void PicoPowerMS(void) |
| 187 | { |
| 188 | int s, tmp; |
| 189 | |
| 190 | memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram); |
| 191 | memset(&Pico.video,0,sizeof(Pico.video)); |
| 192 | memset(&Pico.m,0,sizeof(Pico.m)); |
| 193 | Pico.m.pal = 0; |
| 194 | |
| 195 | // calculate a mask for bank writes. |
| 196 | // ROM loader has aligned the size for us, so this is safe. |
| 197 | s = 0; tmp = Pico.romsize; |
| 198 | while ((tmp >>= 1) != 0) |
| 199 | s++; |
| 200 | if (Pico.romsize > (1 << s)) |
| 201 | s++; |
| 202 | tmp = 1 << s; |
| 203 | bank_mask = (tmp - 1) >> 14; |
| 204 | |
| 205 | PicoReset(); |
| 206 | } |
| 207 | |
| 208 | void PicoMemSetupMS(void) |
| 209 | { |
| 210 | z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0); |
| 211 | z80_map_set(z80_read_map, 0xc000, 0xdfff, Pico.zram, 0); |
| 212 | z80_map_set(z80_read_map, 0xe000, 0xffff, Pico.zram, 0); |
| 213 | |
| 214 | z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1); |
| 215 | z80_map_set(z80_write_map, 0xc000, 0xdfff, Pico.zram, 0); |
| 216 | z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1); |
| 217 | |
| 218 | #ifdef _USE_DRZ80 |
| 219 | drZ80.z80_in = z80_sms_in; |
| 220 | drZ80.z80_out = z80_sms_out; |
| 221 | #endif |
| 222 | #ifdef _USE_CZ80 |
| 223 | Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (UINT32)Pico.rom); |
| 224 | Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (UINT32)Pico.zram); |
| 225 | Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (UINT32)Pico.zram); |
| 226 | Cz80_Set_INPort(&CZ80, z80_sms_in); |
| 227 | Cz80_Set_OUTPort(&CZ80, z80_sms_out); |
| 228 | #endif |
| 229 | } |
| 230 | |
| 231 | void PicoFrameMS(void) |
| 232 | { |
| 233 | struct PicoVideo *pv = &Pico.video; |
| 234 | int is_pal = Pico.m.pal; |
| 235 | int lines = is_pal ? 313 : 262; |
| 236 | int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */ |
| 237 | int cycles_done = 0, cycles_aim = 0; |
| 238 | int lines_vis = 192; |
| 239 | int hint; // Hint counter |
| 240 | int y; |
| 241 | |
| 242 | PicoFrameStartMode4(); |
| 243 | hint = pv->reg[0x0a]; |
| 244 | |
| 245 | for (y = 0; y < lines; y++) |
| 246 | { |
| 247 | pv->v_counter = Pico.m.scanline = y; |
| 248 | |
| 249 | if (y < lines_vis) |
| 250 | PicoLineMode4(y); |
| 251 | |
| 252 | if (y <= lines_vis) |
| 253 | { |
| 254 | if (--hint < 0) |
| 255 | { |
| 256 | hint = pv->reg[0x0a]; |
| 257 | pv->pending_ints |= 2; |
| 258 | if (pv->reg[0] & 0x10) { |
| 259 | elprintf(EL_INTS, "hint"); |
| 260 | z80_int(); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | else if (y == lines_vis + 1) { |
| 265 | pv->pending_ints |= 1; |
| 266 | if (pv->reg[1] & 0x20) { |
| 267 | elprintf(EL_INTS, "vint"); |
| 268 | z80_int(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | cycles_aim += cycles_line; |
| 273 | cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8; |
| 274 | } |
| 275 | |
| 276 | if (PsndOut) |
| 277 | PsndGetSamplesMS(); |
| 278 | } |
| 279 | |
| 280 | void PicoFrameDrawOnlyMS(void) |
| 281 | { |
| 282 | int lines_vis = 192; |
| 283 | int y; |
| 284 | |
| 285 | PicoFrameStartMode4(); |
| 286 | |
| 287 | for (y = 0; y < lines_vis; y++) |
| 288 | PicoLineMode4(y); |
| 289 | } |
| 290 | |