| 1 | #ifdef PPU_CPP |
| 2 | |
| 3 | void PPU::Screen::scanline() { |
| 4 | output = self.output + self.vcounter() * 1024; |
| 5 | if(self.display.interlace && self.field()) output += 512; |
| 6 | |
| 7 | //the first hires pixel of each scanline is transparent |
| 8 | //note: exact value initializations are not confirmed on hardware |
| 9 | math.main.color = get_color(0); |
| 10 | math.sub.color = math.main.color; |
| 11 | |
| 12 | math.main.color_enable = !(self.window.regs.col_main_mask & 1); |
| 13 | math.sub.color_enable = !(self.window.regs.col_sub_mask & 1) && regs.back_color_enable; |
| 14 | |
| 15 | math.transparent = true; |
| 16 | math.addsub_mode = false; |
| 17 | math.color_halve = regs.color_halve && !regs.addsub_mode && math.main.color_enable; |
| 18 | } |
| 19 | |
| 20 | void PPU::Screen::run() { |
| 21 | if(ppu.vcounter() == 0) return; |
| 22 | |
| 23 | bool hires = self.regs.pseudo_hires || self.regs.bgmode == 5 || self.regs.bgmode == 6; |
| 24 | auto sscolor = get_pixel_sub(hires); |
| 25 | auto mscolor = get_pixel_main(); |
| 26 | |
| 27 | *output++ = (self.regs.display_brightness << 15) | (hires ? sscolor : mscolor); |
| 28 | *output++ = (self.regs.display_brightness << 15) | (mscolor); |
| 29 | } |
| 30 | |
| 31 | uint16 PPU::Screen::get_pixel_sub(bool hires) { |
| 32 | if(self.regs.display_disable || (!self.regs.overscan && self.vcounter() >= 225)) return 0; |
| 33 | |
| 34 | unsigned priority = 0; |
| 35 | if(self.bg1.output.sub.priority) { |
| 36 | priority = self.bg1.output.sub.priority; |
| 37 | if(regs.direct_color && (self.regs.bgmode == 3 || self.regs.bgmode == 4 || self.regs.bgmode == 7)) { |
| 38 | math.sub.color = get_direct_color(self.bg1.output.sub.palette, self.bg1.output.sub.tile); |
| 39 | } else { |
| 40 | math.sub.color = get_color(self.bg1.output.sub.palette); |
| 41 | } |
| 42 | } |
| 43 | if(self.bg2.output.sub.priority > priority) { |
| 44 | priority = self.bg2.output.sub.priority; |
| 45 | math.sub.color = get_color(self.bg2.output.sub.palette); |
| 46 | } |
| 47 | if(self.bg3.output.sub.priority > priority) { |
| 48 | priority = self.bg3.output.sub.priority; |
| 49 | math.sub.color = get_color(self.bg3.output.sub.palette); |
| 50 | } |
| 51 | if(self.bg4.output.sub.priority > priority) { |
| 52 | priority = self.bg4.output.sub.priority; |
| 53 | math.sub.color = get_color(self.bg4.output.sub.palette); |
| 54 | } |
| 55 | if(self.sprite.output.sub.priority > priority) { |
| 56 | priority = self.sprite.output.sub.priority; |
| 57 | math.sub.color = get_color(self.sprite.output.sub.palette); |
| 58 | } |
| 59 | math.transparent = (priority == 0); |
| 60 | if(math.transparent) math.sub.color = get_color(0); |
| 61 | |
| 62 | if(!hires) return 0; |
| 63 | if(!math.sub.color_enable) return math.main.color_enable ? math.sub.color : 0; |
| 64 | |
| 65 | return addsub( |
| 66 | math.main.color_enable ? math.sub.color : 0, |
| 67 | math.addsub_mode ? math.main.color : fixed_color() |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | uint16 PPU::Screen::get_pixel_main() { |
| 72 | if(self.regs.display_disable || (!self.regs.overscan && self.vcounter() >= 225)) return 0; |
| 73 | |
| 74 | unsigned priority = 0; |
| 75 | if(self.bg1.output.main.priority) { |
| 76 | priority = self.bg1.output.main.priority; |
| 77 | if(regs.direct_color && (self.regs.bgmode == 3 || self.regs.bgmode == 4 || self.regs.bgmode == 7)) { |
| 78 | math.main.color = get_direct_color(self.bg1.output.main.palette, self.bg1.output.main.tile); |
| 79 | } else { |
| 80 | math.main.color = get_color(self.bg1.output.main.palette); |
| 81 | } |
| 82 | math.sub.color_enable = regs.bg1_color_enable; |
| 83 | } |
| 84 | if(self.bg2.output.main.priority > priority) { |
| 85 | priority = self.bg2.output.main.priority; |
| 86 | math.main.color = get_color(self.bg2.output.main.palette); |
| 87 | math.sub.color_enable = regs.bg2_color_enable; |
| 88 | } |
| 89 | if(self.bg3.output.main.priority > priority) { |
| 90 | priority = self.bg3.output.main.priority; |
| 91 | math.main.color = get_color(self.bg3.output.main.palette); |
| 92 | math.sub.color_enable = regs.bg3_color_enable; |
| 93 | } |
| 94 | if(self.bg4.output.main.priority > priority) { |
| 95 | priority = self.bg4.output.main.priority; |
| 96 | math.main.color = get_color(self.bg4.output.main.palette); |
| 97 | math.sub.color_enable = regs.bg4_color_enable; |
| 98 | } |
| 99 | if(self.sprite.output.main.priority > priority) { |
| 100 | priority = self.sprite.output.main.priority; |
| 101 | math.main.color = get_color(self.sprite.output.main.palette); |
| 102 | math.sub.color_enable = regs.oam_color_enable && self.sprite.output.main.palette >= 192; |
| 103 | } |
| 104 | if(priority == 0) { |
| 105 | math.main.color = get_color(0); |
| 106 | math.sub.color_enable = regs.back_color_enable; |
| 107 | } |
| 108 | |
| 109 | if(!self.window.output.sub.color_enable) math.sub.color_enable = false; |
| 110 | math.main.color_enable = self.window.output.main.color_enable; |
| 111 | if(!math.sub.color_enable) return math.main.color_enable ? math.main.color : 0; |
| 112 | |
| 113 | if(regs.addsub_mode && math.transparent) { |
| 114 | math.addsub_mode = false; |
| 115 | math.color_halve = false; |
| 116 | } else { |
| 117 | math.addsub_mode = regs.addsub_mode; |
| 118 | math.color_halve = regs.color_halve && math.main.color_enable; |
| 119 | } |
| 120 | |
| 121 | return addsub( |
| 122 | math.main.color_enable ? math.main.color : 0, |
| 123 | math.addsub_mode ? math.sub.color : fixed_color() |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | uint16 PPU::Screen::addsub(unsigned x, unsigned y) { |
| 128 | if(!regs.color_mode) { |
| 129 | if(!math.color_halve) { |
| 130 | unsigned sum = x + y; |
| 131 | unsigned carry = (sum - ((x ^ y) & 0x0421)) & 0x8420; |
| 132 | return (sum - carry) | (carry - (carry >> 5)); |
| 133 | } else { |
| 134 | return (x + y - ((x ^ y) & 0x0421)) >> 1; |
| 135 | } |
| 136 | } else { |
| 137 | unsigned diff = x - y + 0x8420; |
| 138 | unsigned borrow = (diff - ((x ^ y) & 0x8420)) & 0x8420; |
| 139 | if(!math.color_halve) { |
| 140 | return (diff - borrow) & (borrow - (borrow >> 5)); |
| 141 | } else { |
| 142 | return (((diff - borrow) & (borrow - (borrow >> 5))) & 0x7bde) >> 1; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | uint16 PPU::Screen::get_color(unsigned palette) { |
| 148 | palette <<= 1; |
| 149 | self.regs.cgram_iaddr = palette; |
| 150 | return ppu.cgram[palette + 0] + (ppu.cgram[palette + 1] << 8); |
| 151 | } |
| 152 | |
| 153 | uint16 PPU::Screen::get_direct_color(unsigned palette, unsigned tile) { |
| 154 | //palette = -------- BBGGGRRR |
| 155 | //tile = ---bgr-- -------- |
| 156 | //output = 0BBb00GG Gg0RRRr0 |
| 157 | return ((palette << 7) & 0x6000) + ((tile >> 0) & 0x1000) |
| 158 | + ((palette << 4) & 0x0380) + ((tile >> 5) & 0x0040) |
| 159 | + ((palette << 2) & 0x001c) + ((tile >> 9) & 0x0002); |
| 160 | } |
| 161 | |
| 162 | uint16 PPU::Screen::fixed_color() const { |
| 163 | return (regs.color_b << 10) | (regs.color_g << 5) | (regs.color_r << 0); |
| 164 | } |
| 165 | |
| 166 | void PPU::Screen::reset() { |
| 167 | regs.addsub_mode = random(false); |
| 168 | regs.direct_color = random(false); |
| 169 | regs.color_mode = random(false); |
| 170 | regs.color_halve = random(false); |
| 171 | regs.bg1_color_enable = random(false); |
| 172 | regs.bg2_color_enable = random(false); |
| 173 | regs.bg3_color_enable = random(false); |
| 174 | regs.bg4_color_enable = random(false); |
| 175 | regs.oam_color_enable = random(false); |
| 176 | regs.back_color_enable = random(false); |
| 177 | regs.color_r = random(0); |
| 178 | regs.color_g = random(0); |
| 179 | regs.color_b = random(0); |
| 180 | } |
| 181 | |
| 182 | PPU::Screen::Screen(PPU& self) : self(self) { |
| 183 | } |
| 184 | |
| 185 | #endif |