summaryrefslogtreecommitdiff
path: root/src/rops.c
blob: e0dcf6928fbf52d6bb5065ca343e8885a9fdc737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "snes9x.h"
#include "ppu.h"
#include "rops.h"

ROPSTRUCT rops[MAX_ROPS];
unsigned int ROpCount;

void doRaster(ROPSTRUCT* rop)
{
   if (!rop)
      return;

   switch (rop->rop)
   {
      case ROP_NOP:
         // NOP
         break;
      case ROP_FIXEDCOLOUR:
         {
            unsigned char col = rop->value & 0x1f;
            // Colour data for fixed colour addition/subtraction
            if (rop->value & 0x80) PPU.FixedColourBlue = col;
            if (rop->value & 0x40) PPU.FixedColourGreen = col;
            if (rop->value & 0x20) PPU.FixedColourRed = col;
         }
         break;
      case ROP_PALETTE:
         {
            // Pallette, colors
            unsigned char col = rop->value & 255;
            IPPU.Blue[col] = (rop->value >> (16 + 10)) & 0x1f;
            IPPU.Green[col] = (rop->value >> (16 + 5)) & 0x1f;
            IPPU.Red[col] = (rop->value >> (16 + 0)) & 0x1f;
            IPPU.ScreenColors[col] = (uint16) BUILD_PIXEL(IPPU.XB[IPPU.Red[col]], IPPU.XB[IPPU.Green[col]],
                  IPPU.XB[IPPU.Blue[col]]);
            IPPU.ColorsChanged = TRUE;
         }
         break;
      case ROP_SCREEN_MODE:
         // Screen mode (0 - 7), background tile sizes and background 3 priority
         PPU.BG[0].BGSize = (rop->value >> 4) & 1;
         PPU.BG[1].BGSize = (rop->value >> 5) & 1;
         PPU.BG[2].BGSize = (rop->value >> 6) & 1;
         PPU.BG[3].BGSize = (rop->value >> 7) & 1;
         PPU.BGMode = rop->value & 7;
         // BJ: BG3Priority only takes effect if BGMode==1 and the bit is set
         PPU.BG3Priority  = ((rop->value & 0x0f) == 0x09);
         break;
      case ROP_BRIGHTNESS:
         PPU.Brightness = rop->value;
         S9xFixColourBrightness();
         if (PPU.Brightness > IPPU.MaxBrightness) IPPU.MaxBrightness = PPU.Brightness;
         IPPU.ColorsChanged = TRUE;
         IPPU.DirectColourMapsNeedRebuild = TRUE;
         break;
      case ROP_FORCE_BLANKING:
         PPU.ForcedBlanking = rop->value;
         IPPU.ColorsChanged = TRUE;
         break;
      case ROP_TILE_ADDRESS:
         PPU.OBJNameBase = (rop->value & 3) << 14;
         PPU.OBJNameSelect = ((rop->value >> 3) & 3) << 13;
         PPU.OBJSizeSelect = (rop->value >> 5) & 7;
         IPPU.OBJChanged = TRUE;
         break;
      case ROP_MOSAIC:
         PPU.Mosaic = (rop->value >> 4) + 1;
         PPU.BGMosaic[0] = (rop->value & 1) && PPU.Mosaic > 1;
         PPU.BGMosaic[1] = (rop->value & 2) && PPU.Mosaic > 1;
         PPU.BGMosaic[2] = (rop->value & 4) && PPU.Mosaic > 1;
         PPU.BGMosaic[3] = (rop->value & 8) && PPU.Mosaic > 1;
         break;
      case ROP_BG_SCSIZE_SCBASE:
         PPU.BG[rop->value >> 16].SCSize = rop->value & 3;
         PPU.BG[rop->value >> 16].SCBase = (rop->value & 0x7c) << 8;
         break;
      case ROP_BG_NAMEBASE:
         PPU.BG[(rop->value >> 16) + 0].NameBase = (rop->value & 7) << 12;
         PPU.BG[(rop->value >> 16) + 1].NameBase = ((rop->value >> 4) & 7) << 12;
         break;
      case ROP_MODE7_ROTATION:
         PPU.Mode7Repeat = rop->value >> 6;
         if (PPU.Mode7Repeat == 1) PPU.Mode7Repeat = 0;
         PPU.Mode7VFlip = (rop->value & 2) >> 1;
         PPU.Mode7HFlip = rop->value & 1;
         break;
      case ROP_BG_WINDOW_ENABLE:
         PPU.ClipWindow1Enable[(rop->value >> 16) + 0] = !!(rop->value & 0x02);
         PPU.ClipWindow1Enable[(rop->value >> 16) + 1] = !!(rop->value & 0x20);
         PPU.ClipWindow2Enable[(rop->value >> 16) + 0] = !!(rop->value & 0x08);
         PPU.ClipWindow2Enable[(rop->value >> 16) + 1] = !!(rop->value & 0x80);
         PPU.ClipWindow1Inside[(rop->value >> 16) + 0] = !(rop->value & 0x01);
         PPU.ClipWindow1Inside[(rop->value >> 16) + 1] = !(rop->value & 0x10);
         PPU.ClipWindow2Inside[(rop->value >> 16) + 0] = !(rop->value & 0x04);
         PPU.ClipWindow2Inside[(rop->value >> 16) + 1] = !(rop->value & 0x40);
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_WINDOW1_LEFT:
         PPU.Window1Left = rop->value;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_WINDOW1_RIGHT:
         PPU.Window1Right = rop->value;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_WINDOW2_LEFT:
         PPU.Window2Left = rop->value;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_WINDOW2_RIGHT:
         PPU.Window2Right = rop->value;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_BG_WINDOW_LOGIC:
         PPU.ClipWindowOverlapLogic[0] = (rop->value & 0x03);
         PPU.ClipWindowOverlapLogic[1] = (rop->value & 0x0c) >> 2;
         PPU.ClipWindowOverlapLogic[2] = (rop->value & 0x30) >> 4;
         PPU.ClipWindowOverlapLogic[3] = (rop->value & 0xc0) >> 6;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_OBJS_WINDOW_LOGIC:
         PPU.ClipWindowOverlapLogic[4] = (rop->value & 0x03);
         PPU.ClipWindowOverlapLogic[5] = (rop->value & 0x0c) >> 2;
         PPU.RecomputeClipWindows = TRUE;
         break;
      case ROP_MAIN_SCREEN_DESIG:
         // Main screen designation (backgrounds 1 - 4 and objects)
         if ((GFX.r212c_s & GFX.r212e_s & 0x1f) != (GFX.r212e_s & rop->value & 0x1f)) PPU.RecomputeClipWindows = TRUE;
         GFX.r212c_s = rop->value;
         break;
      case ROP_SUB_SCREEN_DESIG:
         // Sub-screen designation (backgrounds 1 - 4 and objects)
         if ((GFX.r212d_s & GFX.r212f_s & 0x1f) != (GFX.r212f_s & rop->value & 0x1f)) PPU.RecomputeClipWindows = TRUE;
         GFX.r212d_s = rop->value;
         break;
      case ROP_MAIN_SCREEN_WMASK:
         // Window mask designation for main screen ?
         if ((GFX.r212c_s & GFX.r212e_s & 0x1f) != (GFX.r212c_s & rop->value & 0x1f)) PPU.RecomputeClipWindows = TRUE;
         GFX.r212e_s = rop->value;
         break;
      case ROP_SUB_SCREEN_WMASK:
         // Window mask designation for sub-sreen ?
         if ((GFX.r212d_s & GFX.r212f_s & 0x1f) != (GFX.r212d_s & rop->value & 0x1f)) PPU.RecomputeClipWindows = TRUE;
         GFX.r212f_s = rop->value;
         break;
      case ROP_FIXEDCOL_OR_SCREEN:
         // Fixed colour addition or screen addition
         if ((GFX.r2130_s & 0xf0) != (rop->value & 0xf0)) PPU.RecomputeClipWindows = TRUE;
         GFX.r2130_s = rop->value;
         break;
      case ROP_ADD_OR_SUB_COLOR:
         // Backgrounds 1 - 4, objects and backdrop colour add/sub enable
         GFX.r2131_s = rop->value;
         break;
   }
   rop->rop = 0; // Raster Operation already done, invalidate it
}

bool wouldRasterAlterStatus(ROPSTRUCT* rop)
{
   if (!rop)
      return false;
   switch (rop->rop)
   {
      case ROP_NOP:
         return false;
      case ROP_FIXEDCOLOUR:
         {
            unsigned char col = rop->value & 0x1f;
            // Colour data for fixed colour addition/subtraction
            if ((rop->value & 0x80) && (PPU.FixedColourBlue != col)) return true;
            if ((rop->value & 0x40) && (PPU.FixedColourGreen != col)) return true;
            if ((rop->value & 0x20) && (PPU.FixedColourRed != col)) return true;
            return false;
         }
         break;
      case ROP_PALETTE:
         return true;
      case ROP_SCREEN_MODE:
         return true;
      case ROP_BRIGHTNESS:
         return true;
      case ROP_FORCE_BLANKING:
         return true;
      case ROP_TILE_ADDRESS:
         return true;
      case ROP_MOSAIC:
         return true;
      case ROP_BG_SCSIZE_SCBASE:
         return true;
      case ROP_BG_NAMEBASE:
         return true;
      case ROP_MODE7_ROTATION:
         return true;
      case ROP_BG_WINDOW_ENABLE:
         return true;
      case ROP_WINDOW1_LEFT:
         return true;
      case ROP_WINDOW1_RIGHT:
         return true;
      case ROP_WINDOW2_LEFT:
         return true;
      case ROP_WINDOW2_RIGHT:
         return true;
      case ROP_BG_WINDOW_LOGIC:
         return true;
      case ROP_OBJS_WINDOW_LOGIC:
         return true;
      case ROP_MAIN_SCREEN_DESIG:
         return true;
      case ROP_SUB_SCREEN_DESIG:
         return true;
      case ROP_MAIN_SCREEN_WMASK:
         return true;
      case ROP_SUB_SCREEN_WMASK:
         return true;
      case ROP_FIXEDCOL_OR_SCREEN:
         return true;
      case ROP_ADD_OR_SUB_COLOR:
         return true;
   }
   return true;
}