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