comparison cd_graphics.c @ 2069:8e51c0c3f2e3 segacd

Initial attempt at implementing the Sega CD graphics hardware
author Michael Pavone <pavone@retrodev.com>
date Sun, 30 Jan 2022 19:55:33 -0800
parents
children 598017ef4b0d
comparison
equal deleted inserted replaced
2068:f573f2c31bc9 2069:8e51c0c3f2e3
1 #include "cd_graphics.h"
2 #include "backend.h"
3
4 void cd_graphics_init(segacd_context *cd)
5 {
6 cd->graphics_int_cycle = CYCLE_NEVER;
7 }
8
9 #define BIT_HFLIP 0x8000
10
11 static uint8_t get_src_pixel(segacd_context *cd)
12 {
13 uint16_t x = cd->graphics_x >> 11;
14 uint16_t y = cd->graphics_y >> 11;
15 cd->graphics_x += cd->graphics_dx;
16 cd->graphics_x &= 0xFFFFFF;
17 cd->graphics_y += cd->graphics_dy;
18 cd->graphics_y &= 0xFFFFFF;
19 uint16_t stamp_shift, pixel_mask;
20 uint16_t stamp_num_mask;
21 if (cd->gate_array[GA_STAMP_SIZE] & BIT_STS) {
22 //32x32 stamps
23 stamp_shift = 5;
24 pixel_mask = 0x1F;
25 stamp_num_mask = 0x3FC;
26 } else {
27 //16x16 stamps
28 stamp_shift = 4;
29 pixel_mask = 0xF;
30 stamp_num_mask = 0x3FF;
31 }
32 uint16_t stamp_x = x >> stamp_shift;
33 uint16_t stamp_y = y >> stamp_shift;
34 uint16_t max, base_mask;
35 uint32_t row_shift;
36 if (cd->gate_array[GA_STAMP_SIZE] & BIT_SMS) {
37 max = 4096 >> stamp_shift;
38 base_mask = 0xE000 << ((5 - stamp_shift) << 1);
39 //128 stamps in 32x32 mode, 256 stamps in 16x16 mode
40 row_shift = 12 - stamp_shift;
41 } else {
42 max = 256 >> stamp_shift;
43 base_mask = 0xFFE0 << ((5 - stamp_shift) << 1);
44 //8 stamps in 32x32 mode, 16 stamps in 16x16 mode
45 row_shift = 8 - stamp_shift;
46 }
47 if (stamp_x > max || stamp_y > max) {
48 if (cd->gate_array[GA_STAMP_SIZE] & BIT_RPT) {
49 stamp_x &= max - 1;
50 stamp_y &= max - 1;
51 } else {
52 return 0;
53 }
54 }
55 uint32_t address = (cd->gate_array[GA_STAMP_MAP_BASE] & base_mask) << 1;
56 address += (stamp_y << row_shift) + stamp_x;
57 uint16_t stamp_def = cd->word_ram[address];
58 uint16_t stamp_num = stamp_def & stamp_num_mask;
59 uint16_t pixel_x = x & pixel_mask;
60 uint16_t pixel_y = y & pixel_mask;
61 if (stamp_def & BIT_HFLIP) {
62 pixel_x = pixel_mask - pixel_x;
63 }
64 uint16_t tmp;
65 switch (stamp_def >> 13 & 3)
66 {
67 case 0:
68 break;
69 case 1:
70 tmp = pixel_y;
71 pixel_y = pixel_x;
72 pixel_x = pixel_mask - tmp;
73 break;
74 case 2:
75 tmp = pixel_y;
76 pixel_y = pixel_mask - pixel_x;
77 pixel_x = pixel_mask - tmp;
78 break;
79 case 3:
80 tmp = pixel_y;
81 pixel_y = pixel_mask - pixel_x;
82 pixel_x = tmp;
83 break;
84 }
85 uint16_t cell_x = pixel_x >> 4;
86 uint32_t pixel_address = stamp_num << 6;
87 pixel_address += (pixel_y << 1) + (cell_x << (stamp_shift + 2)) + (pixel_x >> 2 & 1);
88 uint16_t word = cd->word_ram[pixel_address];
89 switch (pixel_x & 3)
90 {
91 default:
92 case 0:
93 return word >> 12;
94 case 1:
95 return word >> 8 & 0xF;
96 case 2:
97 return word >> 4 & 0xF;
98 case 3:
99 return word & 0xF;
100 }
101
102 }
103
104 enum {
105 FETCH_X,
106 FETCH_Y,
107 FETCH_DX,
108 FETCH_DY,
109 PIXEL0,
110 PIXEL1,
111 PIXEL2,
112 PIXEL3,
113 DRAW
114 };
115
116 void draw_pixels(segacd_context *cd)
117 {
118 uint16_t to_draw = 4 - (cd->graphics_dst_x & 3);
119 uint16_t x_end = cd->gate_array[GA_IMAGE_BUFFER_HDOTS] + (cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3);
120 if (cd->graphics_dst_x + to_draw > x_end) {
121 to_draw = cd->gate_array[GA_IMAGE_BUFFER_HDOTS] + (cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3) - cd->graphics_dst_x;
122 }
123 for(uint16_t i = 0; i < to_draw; i++)
124 {
125 uint32_t dst_address = cd->gate_array[GA_IMAGE_BUFFER_START] << 1;
126 dst_address += cd->graphics_dst_y << 4;
127 dst_address += cd->graphics_dst_x >> 2 & 1;
128 dst_address += ((cd->graphics_dst_x >> 3) * cd->gate_array[GA_IMAGE_BUFFER_VCELLS]) << 4;
129 uint16_t pixel_shift = 12 - 4 * (cd->graphics_dst_x & 3);
130 uint16_t pixel = cd->graphics_pixels[i] << pixel_shift;
131 uint16_t src_mask_check = 0xFF << pixel_shift;
132 uint16_t src_mask_keep = ~src_mask_check;
133 switch (cd->gate_array[1] >> 3 & 3)
134 {
135 case 0:
136 //priority mode off
137 cd->word_ram[dst_address] &= src_mask_keep;
138 cd->word_ram[dst_address] |= pixel;
139 break;
140 case 1:
141 //underwrite
142 if (pixel && ! (cd->word_ram[dst_address] & src_mask_check)) {
143 cd->word_ram[dst_address] &= src_mask_keep;
144 cd->word_ram[dst_address] |= pixel;
145 }
146 break;
147 case 3:
148 //overwrite
149 if (pixel) {
150 cd->word_ram[dst_address] &= src_mask_keep;
151 cd->word_ram[dst_address] |= pixel;
152 }
153 break;
154 }
155 }
156 cd->graphics_dst_x += to_draw;
157 if (cd->graphics_dst_x == x_end) {
158 cd->graphics_dst_y++;
159 --cd->gate_array[GA_IMAGE_BUFFER_LINES];
160 cd->graphics_step = FETCH_X;
161 } else {
162 cd->graphics_step = PIXEL0;
163 }
164 }
165
166 #define CHECK_CYCLES cd->graphics_step++; if(cd->graphics_cycle >= cycle) break
167 #define CHECK_ONLY if(cd->graphics_cycle >= cycle) break
168
169 static void do_graphics(segacd_context *cd, uint32_t cycle)
170 {
171 if (!cd->gate_array[GA_IMAGE_BUFFER_LINES]) {
172 return;
173 }
174 while (cd->graphics_cycle < cycle)
175 {
176 switch (cd->graphics_step)
177 {
178 case FETCH_X:
179 cd->graphics_x = cd->word_ram[cd->gate_array[GA_TRACE_VECTOR_BASE] << 1] << 8;
180 cd->graphics_cycle += 3*4;
181 cd->graphics_dst_x = cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3;
182 CHECK_CYCLES;
183 case FETCH_Y:
184 cd->graphics_y = cd->word_ram[cd->gate_array[GA_TRACE_VECTOR_BASE] << 1 + 1] << 8;
185 cd->graphics_cycle += 2*4;
186 CHECK_CYCLES;
187 case FETCH_DX:
188 cd->graphics_dx = cd->word_ram[cd->gate_array[GA_TRACE_VECTOR_BASE] << 1 + 2];
189 if (cd->graphics_dx & 0x8000) {
190 cd->graphics_dx |= 0xFF0000;
191 }
192 cd->graphics_cycle += 2*4;
193 CHECK_CYCLES;
194 case FETCH_DY:
195 cd->graphics_dy = cd->word_ram[cd->gate_array[GA_TRACE_VECTOR_BASE] << 1 + 3];
196 if (cd->graphics_dy & 0x8000) {
197 cd->graphics_dy |= 0xFF0000;
198 }
199 cd->graphics_cycle += 2*4;
200 CHECK_CYCLES;
201 case PIXEL0:
202 cd->graphics_pixels[0] = get_src_pixel(cd);
203 cd->graphics_cycle += 2*4;
204 if ((cd->graphics_dst_x & 3) == 3 || (cd->graphics_dst_x + 1 == cd->gate_array[GA_IMAGE_BUFFER_HDOTS] + (cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3))) {
205 cd->graphics_step = DRAW;
206 CHECK_ONLY;
207 } else {
208 CHECK_CYCLES;
209 }
210 case PIXEL1:
211 cd->graphics_pixels[1] = get_src_pixel(cd);
212 cd->graphics_cycle += 2*4;
213 if ((cd->graphics_dst_x & 3) == 2 || (cd->graphics_dst_x + 2 == cd->gate_array[GA_IMAGE_BUFFER_HDOTS] + (cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3))) {
214 cd->graphics_step = DRAW;
215 CHECK_ONLY;
216 } else {
217 CHECK_CYCLES;
218 }
219 case PIXEL2:
220 cd->graphics_pixels[2] = get_src_pixel(cd);
221 cd->graphics_cycle += 2*4;
222 if ((cd->graphics_dst_x & 3) == 1 || (cd->graphics_dst_x + 3 == cd->gate_array[GA_IMAGE_BUFFER_HDOTS] + (cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3))) {
223 cd->graphics_step = DRAW;
224 CHECK_ONLY;
225 } else {
226 CHECK_CYCLES;
227 }
228 case PIXEL3:
229 cd->graphics_pixels[3] = get_src_pixel(cd);
230 cd->graphics_cycle += 2*4;
231 CHECK_CYCLES;
232 case DRAW:
233 draw_pixels(cd);
234 cd->graphics_cycle += 1*4;
235 if (!cd->gate_array[GA_IMAGE_BUFFER_LINES]) {
236 break;
237 }
238 CHECK_ONLY;
239 }
240 }
241 }
242
243 void cd_graphics_run(segacd_context *cd, uint32_t cycle)
244 {
245 while (cd->graphics_cycle < cycle)
246 {
247 if (cd->gate_array[GA_STAMP_SIZE] & BIT_GRON) {
248 do_graphics(cd, cycle);
249 //end calculation and actual emulated execution time probably don't 100% line up yet
250 //deal with that here for now
251 for(; cd->graphics_cycle < cycle; cd->graphics_cycle += 4)
252 {
253 }
254 if (cd->graphics_cycle >= cd->graphics_int_cycle) {
255 cd->gate_array[GA_STAMP_SIZE] &= ~BIT_GRON;
256 break;
257 }
258 } else {
259 cd->graphics_cycle = cycle;
260 }
261 }
262 }
263 void cd_graphics_start(segacd_context *cd)
264 {
265 if (!(cd->gate_array[GA_STAMP_SIZE] & BIT_GRON)) {
266 printf("grahpics start @ %u\n", cd->graphics_cycle);
267 cd->gate_array[GA_STAMP_SIZE] |= BIT_GRON;
268 //Manual scan is bad, but formula appears to be
269 // vsize * (13 + 2 * hoffset + 9 * (hdots + hoffset - 1))
270 //with an additional 13? cycle setup cost per line
271 uint32_t lines = cd->gate_array[GA_IMAGE_BUFFER_LINES];
272 uint32_t hdots = cd->gate_array[GA_IMAGE_BUFFER_HDOTS];
273 uint32_t hoffset = cd->gate_array[GA_IMAGE_BUFFER_OFFSET] & 3;
274 cd->graphics_int_cycle = cd->graphics_cycle + 4 * lines * (13 + 2 * hoffset + 9 * (hdots + hoffset - 1));
275 cd->graphics_dst_y = cd->gate_array[GA_IMAGE_BUFFER_OFFSET] >> 3;
276 } else {
277 printf("graphics start ignored @ %u\n", cd->graphics_cycle);
278 }
279
280 }