| 1 | #ifdef PPU_CPP |
| 2 | |
| 3 | signed PPU::Background::clip(signed n) { |
| 4 | //13-bit sign extend: --s---nnnnnnnnnn -> ssssssnnnnnnnnnn |
| 5 | return n & 0x2000 ? (n | ~1023) : (n & 1023); |
| 6 | } |
| 7 | |
| 8 | //H = 28 |
| 9 | void PPU::Background::begin_mode7() { |
| 10 | cache.hoffset = self.regs.mode7_hoffset; |
| 11 | cache.voffset = self.regs.mode7_voffset; |
| 12 | } |
| 13 | |
| 14 | void PPU::Background::run_mode7() { |
| 15 | signed a = sclip<16>(self.regs.m7a); |
| 16 | signed b = sclip<16>(self.regs.m7b); |
| 17 | signed c = sclip<16>(self.regs.m7c); |
| 18 | signed d = sclip<16>(self.regs.m7d); |
| 19 | |
| 20 | signed cx = sclip<13>(self.regs.m7x); |
| 21 | signed cy = sclip<13>(self.regs.m7y); |
| 22 | signed hoffset = sclip<13>(cache.hoffset); |
| 23 | signed voffset = sclip<13>(cache.voffset); |
| 24 | |
| 25 | if(Background::x++ & ~255) return; |
| 26 | unsigned x = mosaic.hoffset; |
| 27 | unsigned y = self.bg1.mosaic.voffset; //BG2 vertical mosaic uses BG1 mosaic size |
| 28 | |
| 29 | if(--mosaic.hcounter == 0) { |
| 30 | mosaic.hcounter = regs.mosaic + 1; |
| 31 | mosaic.hoffset += regs.mosaic + 1; |
| 32 | } |
| 33 | |
| 34 | if(self.regs.mode7_hflip) x = 255 - x; |
| 35 | if(self.regs.mode7_vflip) y = 255 - y; |
| 36 | |
| 37 | signed psx = ((a * clip(hoffset - cx)) & ~63) + ((b * clip(voffset - cy)) & ~63) + ((b * y) & ~63) + (cx << 8); |
| 38 | signed psy = ((c * clip(hoffset - cx)) & ~63) + ((d * clip(voffset - cy)) & ~63) + ((d * y) & ~63) + (cy << 8); |
| 39 | |
| 40 | signed px = psx + (a * x); |
| 41 | signed py = psy + (c * x); |
| 42 | |
| 43 | //mask pseudo-FP bits |
| 44 | px >>= 8; |
| 45 | py >>= 8; |
| 46 | |
| 47 | unsigned tile; |
| 48 | unsigned palette; |
| 49 | switch(self.regs.mode7_repeat) { |
| 50 | //screen repetition outside of screen area |
| 51 | case 0: |
| 52 | case 1: |
| 53 | px &= 1023; |
| 54 | py &= 1023; |
| 55 | tile = ppu.vram[((py >> 3) * 128 + (px >> 3)) << 1]; |
| 56 | palette = ppu.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1]; |
| 57 | break; |
| 58 | |
| 59 | //palette color 0 outside of screen area |
| 60 | case 2: |
| 61 | if((px | py) & ~1023) { |
| 62 | palette = 0; |
| 63 | } else { |
| 64 | px &= 1023; |
| 65 | py &= 1023; |
| 66 | tile = ppu.vram[((py >> 3) * 128 + (px >> 3)) << 1]; |
| 67 | palette = ppu.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1]; |
| 68 | } |
| 69 | break; |
| 70 | |
| 71 | //character 0 repetition outside of screen area |
| 72 | case 3: |
| 73 | if((px | py) & ~1023) { |
| 74 | tile = 0; |
| 75 | } else { |
| 76 | px &= 1023; |
| 77 | py &= 1023; |
| 78 | tile = ppu.vram[((py >> 3) * 128 + (px >> 3)) << 1]; |
| 79 | } |
| 80 | palette = ppu.vram[(((tile << 6) + ((py & 7) << 3) + (px & 7)) << 1) + 1]; |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | unsigned priority; |
| 85 | if(id == ID::BG1) { |
| 86 | priority = regs.priority0; |
| 87 | } else if(id == ID::BG2) { |
| 88 | priority = (palette & 0x80 ? regs.priority1 : regs.priority0); |
| 89 | palette &= 0x7f; |
| 90 | } |
| 91 | |
| 92 | if(palette == 0) return; |
| 93 | |
| 94 | if(regs.main_enable) { |
| 95 | output.main.palette = palette; |
| 96 | output.main.priority = priority; |
| 97 | output.main.tile = 0; |
| 98 | } |
| 99 | |
| 100 | if(regs.sub_enable) { |
| 101 | output.sub.palette = palette; |
| 102 | output.sub.priority = priority; |
| 103 | output.sub.tile = 0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #endif |