3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../libsdl/print.h"
24 #include "../libcpu/m68kcpu.h"
26 //#define DEBUG_COPPERLIST
28 static void planar2chunky(uint8_t *rgb, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
31 uint8_t word[planes], chunk;
34 /* we start memory read from the following calulated bitplane offset: */
35 i = y_start * (width / 8);
36 rgb += y_start * width * 3;
37 for (y = y_start; y < y_end; y++) {
38 for (x = 0; x < width; x += 8) {
39 for (p = 0; p < planes; p++)
40 word[p] = bitplanes[p][i];
41 for (b = 0; b < 8; b++) {
42 chunk = (word[planes - 1] >> 7);
43 word[planes - 1] <<= 1;
44 for (p = planes - 2; p >= 0; p--) {
45 chunk = (chunk << 1) | (word[p] >> 7);
48 rgb4 = palette[chunk];
49 *rgb++ = (rgb4 >> 4) & 0xf0;
58 static void planar2chunky2(uint8_t *rgb1, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
60 uint8_t *rgb2, red, green, blue;
62 uint8_t word[planes], chunk;
65 rgb2 = rgb1 + width * 6;
67 /* we start memory read from the following calulated bitplane offset: */
68 i = y_start * (width / 8);
69 rgb1 += 2 * y_start * width * 6;
70 rgb2 += 2 * y_start * width * 6;
71 for (y = y_start; y < y_end; y++) {
72 for (x = 0; x < width; x += 8) {
73 for (p = 0; p < planes; p++)
74 word[p] = bitplanes[p][i];
75 for (b = 0; b < 8; b++) {
76 chunk = (word[planes - 1] >> 7);
77 word[planes - 1] <<= 1;
78 for (p = planes - 2; p >= 0; p--) {
79 chunk = (chunk << 1) | (word[p] >> 7);
82 rgb4 = palette[chunk];
83 red = (rgb4 >> 4) & 0xf0;
106 #define COP1LCH 0x080
107 #define COP1LCL 0x082
108 #define COP2LCH 0x084
109 #define COP2LCL 0x086
110 #define BPL1PTH 0x0e0
111 #define COLOR00 0x180
113 void emul_video(uint8_t *rgb, uint8_t *memory, uint16_t render_palette[], int width, int height, int diwstart, uint16_t *io, int start, int stop, int double_size)
115 uint32_t bitplane[8] = {0, 0, 0, 0, 0, 0, 0, 0};
117 uint16_t palette[16];
120 int row, last_row, line, last_line, from, to;
125 /* special case where all palette entries are white. (unknown reason, maybe due to teleporter travel) */
127 for (i = 0; i < 16; i++) {
128 if (io[i * 2 + 0x180] != 0xfff)
132 /* First set palette as specified in IO space.
133 * This is set with last IRQ, so it is used after VBL.
134 * Later the copper list causes them to be changed */
135 for (i = 0; i < 16; i++) {
136 /* use palette if all value are white, else use the palette that is used during VBL-IRQ */
138 palette[i] = io[i * 2 + 0x180];
140 /* use palette that will be set during IRQ routine.
141 * we need to copy it here, because we don't call IRQ routine for every rendered frame,
142 * so the color registers are not set by IRQ routine for every frame.
144 palette[i] = render_palette[i];
148 /* get copper list start pointer */
149 copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
151 print_error("Copper list pointer not initialized, please fix!\n");
155 #ifdef DEBUG_COPPERLIST
156 printf("Copper list reading from pointer: %06x\n", copperlist);
159 /* parse copper list */
164 if (++count == 100) {
165 print_error("Copper list does not seem to terminate, please fix!\n");
168 c1 = m68k_read_memory_16(copperlist);
169 c2 = m68k_read_memory_16(copperlist + 2);
170 #ifdef DEBUG_COPPERLIST
171 printf("%06x: C1=%04x C2=%04x\n", copperlist, c1, c2);
175 copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
176 #ifdef DEBUG_COPPERLIST
177 printf("switching to 1st copperlist=%06x\n", copperlist);
182 copperlist = (io[COP2LCH] << 16) | io[COP2LCL];
183 #ifdef DEBUG_COPPERLIST
184 printf("switching to 2nd copperlist=%06x\n", copperlist);
190 #ifdef DEBUG_COPPERLIST
191 printf("MOVE 0xdff%03x = 0x%04x\n", c1, c2);
193 /* get bitplane pointers */
194 if (c1 >= BPL1PTH && c1 <= BPL1PTH + 32) {
196 bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0xffff0000) | c2;
198 bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0x0000ffff) | (c2 << 16);
200 /* get color registers */
201 if (c1 >= COLOR00 && c1 <= COLOR00 + 32) {
202 palette[(c1 - COLOR00) / 2] = c2;
206 print_error("We suppport no SKIP command in copper list, please fix!\n");
210 /* get new raster position.
211 * if the value is lower or equal, we add 256 lines
212 * we ignore column, since it is not relevant for this game.
214 row = (c1 >> 8) & ((c2 >> 8) | 0x80);
215 if (row < (last_row & 0xff))
217 #ifdef DEBUG_COPPERLIST
218 printf("WAIT row = 0x%02x & 0x%02x = %d\n", c1 >> 8, (c2 >> 8) | 0x80, row);
221 /* line relative to display window start */
222 line = row - diwstart;
223 /* continue: before display window start */
224 if (line <= last_line)
226 /* render up to height */
229 /* check if bitplane pointers are set */
230 #ifdef DEBUG_COPPERLIST
231 printf("Bitplanes:");
233 for (i = 0; i < 4; i++) {
234 #ifdef DEBUG_COPPERLIST
235 printf(" %06x", bitplane[i]);
237 if (bitplane[i] == 0 || bitplane[i] + width * height / 8 >= 0x80000) {
238 #ifdef DEBUG_COPPERLIST
241 print_error("Bitplane %d in copper list not set or out of range, please fix!\n", i);
244 bitmem[i] = memory + bitplane[i];
246 #ifdef DEBUG_COPPERLIST
249 /* render portion before WAIT line */
250 #ifdef DEBUG_COPPERLIST
251 printf("Render from line %d to line %d\n", last_line, line - 1);
253 #ifdef DEBUG_COPPERLIST
255 for (i = 0; i < 16; i++) {
256 printf(" %03x", palette[i] & 0xfff);
269 planar2chunky2(rgb, bitmem, palette, 4, width, from, to);
271 planar2chunky(rgb, bitmem, palette, 4, width, from, to);
273 /* done if we rendered up to height */
276 /* remeber for next wait command */