Replace printf/fprintf with own print_info() / print_error() using SDL_log
[mercenary-reloaded.git] / src / libvideo / video.c
1 /* video emulation
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include "../libsdl/print.h"
23 #include "video.h"
24 #include "../libcpu/m68kcpu.h"
25
26 //#define DEBUG_COPPERLIST
27
28 static void planar2chunky(uint8_t *rgb, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
29 {
30         int x, y, p, b, i;
31         uint8_t word[planes], chunk;
32         uint16_t rgb4;
33
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);
46                                         word[p] <<= 1;
47                                 }
48                                 rgb4 = palette[chunk];
49                                 *rgb++ = (rgb4 >> 4) & 0xf0;
50                                 *rgb++ = rgb4 & 0xf0;
51                                 *rgb++ = rgb4 << 4;
52                         }
53                         i++;
54                 }
55         }
56 }
57
58 static void planar2chunky2(uint8_t *rgb1, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
59 {
60         uint8_t *rgb2, red, green, blue;
61         int x, y, p, b, i;
62         uint8_t word[planes], chunk;
63         uint16_t rgb4;
64
65         rgb2 = rgb1 + width * 6;
66
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);
80                                         word[p] <<= 1;
81                                 }
82                                 rgb4 = palette[chunk];
83                                 red = (rgb4 >> 4) & 0xf0;
84                                 green = rgb4 & 0xf0;
85                                 blue = rgb4 << 4;
86                                 *rgb1++ = red;
87                                 *rgb1++ = green;
88                                 *rgb1++ = blue;
89                                 *rgb1++ = red;
90                                 *rgb1++ = green;
91                                 *rgb1++ = blue;
92                                 *rgb2++ = red;
93                                 *rgb2++ = green;
94                                 *rgb2++ = blue;
95                                 *rgb2++ = red;
96                                 *rgb2++ = green;
97                                 *rgb2++ = blue;
98                         }
99                         i++;
100                 }
101                 rgb1 += width * 6;
102                 rgb2 += width * 6;
103         }
104 }
105
106 #define COP1LCH         0x080
107 #define COP1LCL         0x082
108 #define COP2LCH         0x084
109 #define COP2LCL         0x086
110 #define BPL1PTH         0x0e0
111 #define COLOR00         0x180
112
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)
114 {
115         uint32_t bitplane[8] = {0, 0, 0, 0, 0, 0, 0, 0};
116         uint8_t *bitmem[8];
117         uint16_t palette[16];
118         uint32_t copperlist;
119         uint16_t c1, c2;
120         int row, last_row, line, last_line, from, to;
121         int count;
122         int i;
123         int all_white;
124
125         /* special case where all palette entries are white. (unknown reason, maybe due to teleporter travel) */
126         all_white = 1;
127         for (i = 0; i < 16; i++) {
128                 if (io[i * 2 + 0x180] != 0xfff)
129                         all_white = 0;
130         }
131
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 */
137                 if (all_white)
138                         palette[i] = io[i * 2 + 0x180];
139                 else {
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.
143                          */
144                         palette[i] = render_palette[i];
145                 }
146         }
147
148         /* get copper list start pointer */
149         copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
150         if (!copperlist) {
151                 print_error("Copper list pointer not initialized, please fix!\n");
152                 return;
153         }
154
155 #ifdef DEBUG_COPPERLIST
156         printf("Copper list reading from pointer: %06x\n", copperlist);
157 #endif
158
159         /* parse copper list */
160         count = 0;
161         last_row = 0;
162         last_line = 0;
163         while (42) {
164                 if (++count == 100) {
165                         print_error("Copper list does not seem to terminate, please fix!\n");
166                         return;
167                 }
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);
172 #endif
173                 copperlist += 4;
174                 if (c1 == 0x0088) {
175                         copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
176 #ifdef DEBUG_COPPERLIST
177                         printf("switching to 1st copperlist=%06x\n", copperlist);
178 #endif
179                         continue;
180                 }
181                 if (c1 == 0x008a) {
182                         copperlist = (io[COP2LCH] << 16) | io[COP2LCL];
183 #ifdef DEBUG_COPPERLIST
184                         printf("switching to 2nd copperlist=%06x\n", copperlist);
185 #endif
186                         continue;
187                 }
188                 if (!(c1 & 1)) {
189                         /* MOVE */
190 #ifdef DEBUG_COPPERLIST
191                         printf("MOVE 0xdff%03x = 0x%04x\n", c1, c2);
192 #endif
193                         /* get bitplane pointers */
194                         if (c1 >= BPL1PTH && c1 <= BPL1PTH + 32) {
195                                 if ((c1 & 2))
196                                         bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0xffff0000) | c2;
197                                 else
198                                         bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0x0000ffff) | (c2 << 16);
199                         }
200                         /* get color registers */
201                         if (c1 >= COLOR00 && c1 <= COLOR00 + 32) {
202                                 palette[(c1 - COLOR00) / 2] = c2;
203                         }
204                 } else {
205                         if ((c2 & 1)) {
206                                 print_error("We suppport no SKIP command in copper list, please fix!\n");
207                                 continue;
208                         }
209                         /* WAIT */
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.
213                          */
214                         row = (c1 >> 8) & ((c2 >> 8) | 0x80);
215                         if (row < (last_row & 0xff))
216                                 row |= 0x100;
217 #ifdef DEBUG_COPPERLIST
218                         printf("WAIT row = 0x%02x & 0x%02x = %d\n", c1 >> 8, (c2 >> 8) | 0x80, row);
219 #endif
220                         last_row = row;
221                         /* line relative to display window start */
222                         line = row - diwstart;
223                         /* continue: before display window start */
224                         if (line <= last_line)
225                                 continue;
226                         /* render up to height */
227                         if (line >= height)
228                                 line = height;
229                         /* check if bitplane pointers are set */
230 #ifdef DEBUG_COPPERLIST
231                         printf("Bitplanes:");
232 #endif
233                         for (i = 0; i < 4; i++) {
234 #ifdef DEBUG_COPPERLIST
235                                 printf(" %06x", bitplane[i]);
236 #endif
237                                 if (bitplane[i] == 0 || bitplane[i] + width * height / 8 >= 0x80000) {
238 #ifdef DEBUG_COPPERLIST
239                                         printf("\n");
240 #endif
241                                         print_error("Bitplane %d in copper list not set or out of range, please fix!\n", i);
242                                         return;
243                                 }
244                                 bitmem[i] = memory + bitplane[i];
245                         }
246 #ifdef DEBUG_COPPERLIST
247                         printf("\n");
248 #endif
249                         /* render portion before WAIT line */
250 #ifdef DEBUG_COPPERLIST
251                         printf("Render from line %d to line %d\n", last_line, line - 1);
252 #endif
253 #ifdef DEBUG_COPPERLIST
254                         printf("Palette:");
255                         for (i = 0; i < 16; i++) {
256                                 printf(" %03x", palette[i] & 0xfff);
257                         }
258                         printf("\n");
259 #endif
260
261                         from = last_line;
262                         to = line;
263                         if (start > from)
264                                 from = start;
265                         if (stop < to)
266                                 to = stop;
267                         if (to > from) {
268                                 if (double_size)
269                                         planar2chunky2(rgb, bitmem, palette, 4, width, from, to);
270                                 else
271                                         planar2chunky(rgb, bitmem, palette, 4, width, from, to);
272                         }
273                         /* done if we rendered up to height */
274                         if (line == height)
275                                 break;
276                         /* remeber for next wait command */
277                         last_row = row;
278                         last_line = line;
279                 }
280         }
281
282 }