Use double pixel size to render amiga video (2x2)
[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 "video.h"
23 #include "../libcpu/m68kcpu.h"
24
25 //#define DEBUG_COPPERLIST
26
27 static void planar2chunky(uint8_t *rgb, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
28 {
29         int x, y, p, b, i;
30         uint8_t word[planes], chunk;
31         uint16_t rgb4;
32
33         /* we start memory read from the following calulated bitplane offset: */
34         i = y_start * (width / 8);
35         rgb += y_start * width * 3;
36         for (y = y_start; y < y_end; y++) {
37                 for (x = 0; x < width; x += 8) {
38                         for (p = 0; p < planes; p++)
39                                 word[p] = bitplanes[p][i];
40                         for (b = 0; b < 8; b++) {
41                                 chunk = (word[planes - 1] >> 7);
42                                 word[planes - 1] <<= 1;
43                                 for (p = planes - 2; p >= 0; p--) {
44                                         chunk = (chunk << 1) | (word[p] >> 7);
45                                         word[p] <<= 1;
46                                 }
47                                 rgb4 = palette[chunk];
48                                 *rgb++ = (rgb4 >> 4) & 0xf0;
49                                 *rgb++ = rgb4 & 0xf0;
50                                 *rgb++ = rgb4 << 4;
51                         }
52                         i++;
53                 }
54         }
55 }
56
57 static void planar2chunky2(uint8_t *rgb1, uint8_t *bitplanes[], uint16_t palette[], int planes, int width, int y_start, int y_end)
58 {
59         uint8_t *rgb2, red, green, blue;
60         int x, y, p, b, i;
61         uint8_t word[planes], chunk;
62         uint16_t rgb4;
63
64         rgb2 = rgb1 + width * 6;
65
66         /* we start memory read from the following calulated bitplane offset: */
67         i = y_start * (width / 8);
68         rgb1 += 2 * y_start * width * 6;
69         rgb2 += 2 * y_start * width * 6;
70         for (y = y_start; y < y_end; y++) {
71                 for (x = 0; x < width; x += 8) {
72                         for (p = 0; p < planes; p++)
73                                 word[p] = bitplanes[p][i];
74                         for (b = 0; b < 8; b++) {
75                                 chunk = (word[planes - 1] >> 7);
76                                 word[planes - 1] <<= 1;
77                                 for (p = planes - 2; p >= 0; p--) {
78                                         chunk = (chunk << 1) | (word[p] >> 7);
79                                         word[p] <<= 1;
80                                 }
81                                 rgb4 = palette[chunk];
82                                 red = (rgb4 >> 4) & 0xf0;
83                                 green = rgb4 & 0xf0;
84                                 blue = rgb4 << 4;
85                                 *rgb1++ = red;
86                                 *rgb1++ = green;
87                                 *rgb1++ = blue;
88                                 *rgb1++ = red;
89                                 *rgb1++ = green;
90                                 *rgb1++ = blue;
91                                 *rgb2++ = red;
92                                 *rgb2++ = green;
93                                 *rgb2++ = blue;
94                                 *rgb2++ = red;
95                                 *rgb2++ = green;
96                                 *rgb2++ = blue;
97                         }
98                         i++;
99                 }
100                 rgb1 += width * 6;
101                 rgb2 += width * 6;
102         }
103 }
104
105 #define COP1LCH         0x080
106 #define COP1LCL         0x082
107 #define COP2LCH         0x084
108 #define COP2LCL         0x086
109 #define BPL1PTH         0x0e0
110 #define COLOR00         0x180
111
112 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)
113 {
114         uint32_t bitplane[8] = {0, 0, 0, 0, 0, 0, 0, 0};
115         uint8_t *bitmem[8];
116         uint16_t palette[16];
117         uint32_t copperlist;
118         uint16_t c1, c2;
119         int row, last_row, line, last_line, from, to;
120         int count;
121         int i;
122         int all_white;
123
124         /* special case where all palette entries are white. (unknown reason, maybe due to teleporter travel) */
125         all_white = 1;
126         for (i = 0; i < 16; i++) {
127                 if (io[i * 2 + 0x180] != 0xfff)
128                         all_white = 0;
129         }
130
131         /* First set palette as specified in IO space.
132          * This is set with last IRQ, so it is used after VBL.
133          * Later the copper list causes them to be changed */
134         for (i = 0; i < 16; i++) {
135                 /* use palette if all value are white, else use the palette that is used during VBL-IRQ */
136                 if (all_white)
137                         palette[i] = io[i * 2 + 0x180];
138                 else {
139                         /* use palette that will be set during IRQ routine.
140                          * we need to copy it here, because we don't call IRQ routine for every rendered frame,
141                          * so the color registers are not set by IRQ routine for every frame.
142                          */
143                         palette[i] = render_palette[i];
144                 }
145         }
146
147         /* get copper list start pointer */
148         copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
149         if (!copperlist) {
150                 fprintf(stderr, "Copper list pointer not initialized, please fix!\n");
151                 return;
152         }
153
154 #ifdef DEBUG_COPPERLIST
155         printf("Copper list reading from pointer: %06x\n", copperlist);
156 #endif
157
158         /* parse copper list */
159         count = 0;
160         last_row = 0;
161         last_line = 0;
162         while (42) {
163                 if (++count == 100) {
164                         fprintf(stderr, "Copper list does not seem to terminate, please fix!\n");
165                         return;
166                 }
167                 c1 = m68k_read_memory_16(copperlist);
168                 c2 = m68k_read_memory_16(copperlist + 2);
169 #ifdef DEBUG_COPPERLIST
170                 printf("%06x: C1=%04x C2=%04x\n", copperlist, c1, c2);
171 #endif
172                 copperlist += 4;
173                 if (c1 == 0x0088) {
174                         copperlist = (io[COP1LCH] << 16) | io[COP1LCL];
175 #ifdef DEBUG_COPPERLIST
176                         printf("switching to 1st copperlist=%06x\n", copperlist);
177 #endif
178                         continue;
179                 }
180                 if (c1 == 0x008a) {
181                         copperlist = (io[COP2LCH] << 16) | io[COP2LCL];
182 #ifdef DEBUG_COPPERLIST
183                         printf("switching to 2nd copperlist=%06x\n", copperlist);
184 #endif
185                         continue;
186                 }
187                 if (!(c1 & 1)) {
188                         /* MOVE */
189 #ifdef DEBUG_COPPERLIST
190                         printf("MOVE 0xdff%03x = 0x%04x\n", c1, c2);
191 #endif
192                         /* get bitplane pointers */
193                         if (c1 >= BPL1PTH && c1 <= BPL1PTH + 32) {
194                                 if ((c1 & 2))
195                                         bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0xffff0000) | c2;
196                                 else
197                                         bitplane[(c1 - BPL1PTH) / 4] = (bitplane[(c1 - BPL1PTH) / 4] & 0x0000ffff) | (c2 << 16);
198                         }
199                         /* get color registers */
200                         if (c1 >= COLOR00 && c1 <= COLOR00 + 32) {
201                                 palette[(c1 - COLOR00) / 2] = c2;
202                         }
203                 } else {
204                         if ((c2 & 1)) {
205                                 fprintf(stderr, "We suppport no SKIP command in copper list, please fix!\n");
206                                 continue;
207                         }
208                         /* WAIT */
209                         /* get new raster position.
210                          * if the value is lower or equal, we add 256 lines
211                          * we ignore column, since it is not relevant for this game.
212                          */
213                         row = (c1 >> 8) & ((c2 >> 8) | 0x80);
214                         if (row < (last_row & 0xff))
215                                 row |= 0x100;
216 #ifdef DEBUG_COPPERLIST
217                         printf("WAIT row = 0x%02x & 0x%02x = %d\n", c1 >> 8, (c2 >> 8) | 0x80, row);
218 #endif
219                         last_row = row;
220                         /* line relative to display window start */
221                         line = row - diwstart;
222                         /* continue: before display window start */
223                         if (line <= last_line)
224                                 continue;
225                         /* render up to height */
226                         if (line >= height)
227                                 line = height;
228                         /* check if bitplane pointers are set */
229 #ifdef DEBUG_COPPERLIST
230                         printf("Bitplanes:");
231 #endif
232                         for (i = 0; i < 4; i++) {
233 #ifdef DEBUG_COPPERLIST
234                                 printf(" %06x", bitplane[i]);
235 #endif
236                                 if (bitplane[i] == 0 || bitplane[i] + width * height / 8 >= 0x80000) {
237                                         printf("\n");
238                                         fprintf(stderr, "Bitplane %d in copper list not set or out of range, please fix!\n", i);
239                                         return;
240                                 }
241                                 bitmem[i] = memory + bitplane[i];
242                         }
243 #ifdef DEBUG_COPPERLIST
244                         printf("\n");
245 #endif
246                         /* render portion before WAIT line */
247 #ifdef DEBUG_COPPERLIST
248                         printf("Render from line %d to line %d\n", last_line, line - 1);
249 #endif
250 #ifdef DEBUG_COPPERLIST
251                         printf("Palette:");
252                         for (i = 0; i < 16; i++) {
253                                 printf(" %03x", palette[i] & 0xfff);
254                         }
255                         printf("\n");
256 #endif
257
258                         from = last_line;
259                         to = line;
260                         if (start > from)
261                                 from = start;
262                         if (stop < to)
263                                 to = stop;
264                         if (to > from) {
265                                 if (double_size)
266                                         planar2chunky2(rgb, bitmem, palette, 4, width, from, to);
267                                 else
268                                         planar2chunky(rgb, bitmem, palette, 4, width, from, to);
269                         }
270                         /* done if we rendered up to height */
271                         if (line == height)
272                                 break;
273                         /* remeber for next wait command */
274                         last_row = row;
275                         last_line = line;
276                 }
277         }
278
279 }