| 1 | /* |
| 2 | * TODO: |
| 3 | * - TMS9918 modes? |
| 4 | * - gg mode? |
| 5 | * - column scroll (reg 0 bit7) |
| 6 | * - 224/240 line modes |
| 7 | * - doubled sprites |
| 8 | */ |
| 9 | #include "pico_int.h" |
| 10 | |
| 11 | static void (*FinalizeLineM4)(int line); |
| 12 | static int skip_next_line; |
| 13 | static int screen_offset; |
| 14 | |
| 15 | #define PLANAR_PIXEL(x,p) \ |
| 16 | t = pack & (0x80808080 >> p); \ |
| 17 | if (t) { \ |
| 18 | t = ((t >> (7-p)) | (t >> (14-p)) | (t >> (21-p)) | (t >> (28-p))) & 0x0f; \ |
| 19 | pd[x] = pal|t; \ |
| 20 | } |
| 21 | |
| 22 | static int TileNormM4(int sx, int addr, int pal) |
| 23 | { |
| 24 | unsigned char *pd = HighCol + sx; |
| 25 | unsigned int pack, t; |
| 26 | |
| 27 | pack = *(unsigned int *)(Pico.vram + addr); /* Get 4 bitplanes / 8 pixels */ |
| 28 | if (pack) |
| 29 | { |
| 30 | PLANAR_PIXEL(0, 0) |
| 31 | PLANAR_PIXEL(1, 1) |
| 32 | PLANAR_PIXEL(2, 2) |
| 33 | PLANAR_PIXEL(3, 3) |
| 34 | PLANAR_PIXEL(4, 4) |
| 35 | PLANAR_PIXEL(5, 5) |
| 36 | PLANAR_PIXEL(6, 6) |
| 37 | PLANAR_PIXEL(7, 7) |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | return 1; /* Tile blank */ |
| 42 | } |
| 43 | |
| 44 | static int TileFlipM4(int sx,int addr,int pal) |
| 45 | { |
| 46 | unsigned char *pd = HighCol + sx; |
| 47 | unsigned int pack, t; |
| 48 | |
| 49 | pack = *(unsigned int *)(Pico.vram + addr); /* Get 4 bitplanes / 8 pixels */ |
| 50 | if (pack) |
| 51 | { |
| 52 | PLANAR_PIXEL(0, 7) |
| 53 | PLANAR_PIXEL(1, 6) |
| 54 | PLANAR_PIXEL(2, 5) |
| 55 | PLANAR_PIXEL(3, 4) |
| 56 | PLANAR_PIXEL(4, 3) |
| 57 | PLANAR_PIXEL(5, 2) |
| 58 | PLANAR_PIXEL(6, 1) |
| 59 | PLANAR_PIXEL(7, 0) |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | return 1; /* Tile blank */ |
| 64 | } |
| 65 | |
| 66 | static void draw_sprites(int scanline) |
| 67 | { |
| 68 | struct PicoVideo *pv = &Pico.video; |
| 69 | unsigned int sprites_addr[8]; |
| 70 | unsigned int sprites_x[8]; |
| 71 | unsigned char *sat; |
| 72 | int xoff = 8; // relative to HighCol, which is (screen - 8) |
| 73 | int sprite_base, addr_mask; |
| 74 | int i, s, h; |
| 75 | |
| 76 | if (pv->reg[0] & 8) |
| 77 | xoff = 0; |
| 78 | |
| 79 | sat = (unsigned char *)Pico.vram + ((pv->reg[5] & 0x7e) << 7); |
| 80 | if (pv->reg[1] & 2) { |
| 81 | addr_mask = 0xfe; h = 16; |
| 82 | } else { |
| 83 | addr_mask = 0xff; h = 8; |
| 84 | } |
| 85 | sprite_base = (pv->reg[6] & 4) << (13-2-1); |
| 86 | |
| 87 | for (i = s = 0; i < 64 && s < 8; i++) |
| 88 | { |
| 89 | int y; |
| 90 | y = sat[i] + 1; |
| 91 | if (y == 0xd1) |
| 92 | break; |
| 93 | if (y + h <= scanline || scanline < y) |
| 94 | continue; // not on this line |
| 95 | |
| 96 | sprites_x[s] = xoff + sat[0x80 + i*2]; |
| 97 | sprites_addr[s] = sprite_base + ((sat[0x80 + i*2 + 1] & addr_mask) << (5-1)) + |
| 98 | ((scanline - y) << (2-1)); |
| 99 | s++; |
| 100 | } |
| 101 | |
| 102 | // now draw all sprites backwards |
| 103 | for (--s; s >= 0; s--) |
| 104 | TileNormM4(sprites_x[s], sprites_addr[s], 0x10); |
| 105 | } |
| 106 | |
| 107 | // tilex_ty_prio merged to reduce register pressure |
| 108 | static void draw_strip(const unsigned short *nametab, int dx, int cells, int tilex_ty_prio) |
| 109 | { |
| 110 | int oldcode = -1, blank = -1; // The tile we know is blank |
| 111 | int addr = 0, pal = 0; |
| 112 | |
| 113 | // Draw tiles across screen: |
| 114 | for (; cells > 0; dx += 8, tilex_ty_prio++, cells--) |
| 115 | { |
| 116 | int code, zero; |
| 117 | |
| 118 | code = nametab[tilex_ty_prio & 0x1f]; |
| 119 | if (code == blank) |
| 120 | continue; |
| 121 | if ((code ^ tilex_ty_prio) & 0x1000) // priority differs? |
| 122 | continue; |
| 123 | |
| 124 | if (code != oldcode) { |
| 125 | oldcode = code; |
| 126 | // Get tile address/2: |
| 127 | addr = (code & 0x1ff) << 4; |
| 128 | addr += tilex_ty_prio >> 16; |
| 129 | if (code & 0x0400) |
| 130 | addr ^= 0xe; // Y-flip |
| 131 | |
| 132 | pal = (code>>7) & 0x10; |
| 133 | } |
| 134 | |
| 135 | if (code&0x0200) zero = TileFlipM4(dx, addr, pal); |
| 136 | else zero = TileNormM4(dx, addr, pal); |
| 137 | |
| 138 | if (zero) |
| 139 | blank = code; // We know this tile is blank now |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void DrawDisplayM4(int scanline) |
| 144 | { |
| 145 | struct PicoVideo *pv = &Pico.video; |
| 146 | unsigned short *nametab; |
| 147 | int line, tilex, dx, ty, cells; |
| 148 | int cellskip = 0; // XXX |
| 149 | int maxcells = 32; |
| 150 | |
| 151 | // Find the line in the name table |
| 152 | line = pv->reg[9] + scanline; // vscroll + scanline |
| 153 | if (line >= 224) |
| 154 | line -= 224; |
| 155 | |
| 156 | // Find name table: |
| 157 | nametab = Pico.vram; |
| 158 | nametab += (pv->reg[2] & 0x0e) << (10-1); |
| 159 | nametab += (line>>3) << (6-1); |
| 160 | |
| 161 | dx = pv->reg[8]; // hscroll |
| 162 | if (scanline < 16 && (pv->reg[0] & 0x40)) |
| 163 | dx = 0; // hscroll disabled for top 2 rows |
| 164 | |
| 165 | tilex = ((-dx >> 3) + cellskip) & 0x1f; |
| 166 | ty = (line & 7) << 1; // Y-Offset into tile |
| 167 | cells = maxcells - cellskip; |
| 168 | |
| 169 | dx = ((dx - 1) & 7) + 1; |
| 170 | if (dx != 8) |
| 171 | cells++; // have hscroll, need to draw 1 cell more |
| 172 | dx += cellskip << 3; |
| 173 | |
| 174 | // low priority tiles |
| 175 | if (PicoDrawMask & PDRAW_LAYERB_ON) |
| 176 | draw_strip(nametab, dx, cells, tilex | 0x0000 | (ty << 16)); |
| 177 | |
| 178 | // sprites |
| 179 | if (PicoDrawMask & PDRAW_SPRITES_LOW_ON) |
| 180 | draw_sprites(scanline); |
| 181 | |
| 182 | // high priority tiles (use virtual layer switch just for fun) |
| 183 | if (PicoDrawMask & PDRAW_LAYERA_ON) |
| 184 | draw_strip(nametab, dx, cells, tilex | 0x1000 | (ty << 16)); |
| 185 | |
| 186 | if (pv->reg[0] & 0x20) |
| 187 | // first column masked |
| 188 | ((int *)HighCol)[2] = ((int *)HighCol)[3] = 0xe0e0e0e0; |
| 189 | } |
| 190 | |
| 191 | void PicoFrameStartMode4(void) |
| 192 | { |
| 193 | int lines = 192; |
| 194 | skip_next_line = 0; |
| 195 | screen_offset = 24; |
| 196 | rendstatus = 0; |
| 197 | |
| 198 | if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18)) { |
| 199 | if (Pico.video.reg[1] & 0x08) { |
| 200 | rendstatus |= PDRAW_240LINES; |
| 201 | screen_offset = 0; |
| 202 | lines = 240; |
| 203 | } |
| 204 | else { |
| 205 | screen_offset = 8; |
| 206 | lines = 224; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (rendstatus != rendstatus_old) { |
| 211 | rendstatus_old = rendstatus; |
| 212 | emu_video_mode_change(screen_offset, lines, 1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void PicoLineMode4(int line) |
| 217 | { |
| 218 | if (skip_next_line > 0) { |
| 219 | skip_next_line--; |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (PicoScanBegin != NULL) |
| 224 | skip_next_line = PicoScanBegin(line + screen_offset); |
| 225 | |
| 226 | // Draw screen: |
| 227 | BackFill(Pico.video.reg[7] & 0x0f, 0); |
| 228 | if (Pico.video.reg[1] & 0x40) |
| 229 | DrawDisplayM4(line); |
| 230 | |
| 231 | if (FinalizeLineM4 != NULL) |
| 232 | FinalizeLineM4(line); |
| 233 | |
| 234 | if (PicoScanEnd != NULL) |
| 235 | skip_next_line = PicoScanEnd(line + screen_offset); |
| 236 | } |
| 237 | |
| 238 | void PicoDoHighPal555M4(void) |
| 239 | { |
| 240 | unsigned int *spal=(void *)Pico.cram; |
| 241 | unsigned int *dpal=(void *)HighPal; |
| 242 | unsigned int t; |
| 243 | int i; |
| 244 | |
| 245 | Pico.m.dirtyPal = 0; |
| 246 | |
| 247 | /* cram is always stored as shorts, even though real hardware probably uses bytes */ |
| 248 | for (i = 0x20/2; i > 0; i--, spal++, dpal++) { |
| 249 | t = *spal; |
| 250 | #ifdef USE_BGR555 |
| 251 | t = ((t & 0x00030003)<< 3) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)<<10); |
| 252 | #else |
| 253 | t = ((t & 0x00030003)<<14) | ((t & 0x000c000c)<<7) | ((t & 0x00300030)>>1); |
| 254 | #endif |
| 255 | t |= t >> 2; |
| 256 | t |= (t >> 4) & 0x08610861; |
| 257 | *dpal = t; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void FinalizeLineRGB555M4(int line) |
| 262 | { |
| 263 | if (Pico.m.dirtyPal) |
| 264 | PicoDoHighPal555M4(); |
| 265 | |
| 266 | // standard FinalizeLine can finish it for us, |
| 267 | // with features like scaling and such |
| 268 | FinalizeLineRGB555(0, line); |
| 269 | } |
| 270 | |
| 271 | static void FinalizeLine8bitM4(int line) |
| 272 | { |
| 273 | unsigned char *pd = DrawLineDest; |
| 274 | |
| 275 | if (!(PicoOpt & POPT_DIS_32C_BORDER)) |
| 276 | pd += 32; |
| 277 | |
| 278 | memcpy32((int *)pd, (int *)(HighCol+8), 256/4); |
| 279 | } |
| 280 | |
| 281 | void PicoDrawSetColorFormatMode4(int which) |
| 282 | { |
| 283 | switch (which) |
| 284 | { |
| 285 | case 2: FinalizeLineM4 = FinalizeLine8bitM4; break; |
| 286 | case 1: FinalizeLineM4 = FinalizeLineRGB555M4; break; |
| 287 | default:FinalizeLineM4 = NULL; break; |
| 288 | } |
| 289 | } |
| 290 | |