-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.cpp
505 lines (415 loc) · 10.7 KB
/
video.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* video.cpp
*
* Author: Massimiliano Petra <[email protected]> February, 2020
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#include <stdio.h>
#include "video.h"
#include "gui.h"
#include "memory.h"
// Gui debug
extern uint8_t guidebug;
//The window we'll be rendering to
SDL_Window* window = NULL;
//The renderer (used by DSL2 drawing API)
SDL_Renderer* Main_Renderer;
//The surface contained by the window0
SDL_Surface* screenSurface = NULL;
//Video pixels
uint32_t *pixels = NULL;
// Screen switches
uint8_t screen_switches = 0;
uint32_t color = 0x00ffffff;
uint16_t page_row_memory_map[] = {
0x000, 0x080, 0x100, 0x180, 0x200, 0x280, 0x300,0x380,
0x028, 0x0A8, 0x128, 0x1A8, 0x228, 0x2A8, 0x328,0x3A8,
0x050, 0x0D0, 0x150, 0x1D0, 0x250, 0x2D0, 0x350,0x3D0
};
uint32_t palette[] = {
0x00000000,0x0099035F,0X004204E1,0X00CA13FE,
0X00007310,0X007F7F7F,0X002497FF,0X00AAA2FF,
0X004F5101,0X00F05C00,0X00BEBEBE,0X00FF85FF,
0X0012CA07,0X00CED413,0X0051F595,0x00FFFFFF};
uint32_t hpalette0[] = {
0X00CA13FE,0X002497FF};
uint32_t hpalette1[] = {
0X0012CA07,0X00F05C00};
void clearscreen()
{
// Clear screen
SDL_FillRect(screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0x00, 0x00));
SDL_UpdateWindowSurface(window);
}
uint16_t flash_count = 0;
void video_refresh(uint8_t *RAM)
{
uint8_t c_val;
uint8_t p,p_next;
uint8_t char_byte;
uint32_t v;
int r,c;
int text_r,text_c;
int gr_r,gr_c;
int mode;
uint16_t base_address=0;
uint16_t base_address_text=0;
uint8_t hcolor;
// Flash count
flash_count = (flash_count +1) &0x3f;
if ((screen_switches & _TEXT_GRAPHICS) == 0)
{
// TEXT MODE
if ((screen_switches & _PAGE1_PAGE2) == 0)
base_address = _LO_RES1;
else
base_address = _LO_RES2;
for (r=0;r<SCREEN_HEIGHT/2;r++)
{
text_r = r / 8;
for (c=0;c<screenSurface->w/2;c++)
{
text_c = c / 7;
if ((c % 7) == 0)
{
c_val = RAM[base_address+page_row_memory_map[text_r]+text_c];
mode = c_val >> 6;
// reduce to 5x7 matrix
//c_val = c_val & 0x3F;
char_byte = _CHARROM[c_val*8+(r&0x07)];
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
// Inverse
if (mode == 0)
char_byte = char_byte ^ 0xFF;
// Flashing
if ((mode == 1) && (flash_count >= 0x1f))
char_byte = char_byte ^ 0xFF;
}
if (REVERSE_CHAR_ROM)
{
if (char_byte & 0x01)
v = color;
else
v = 0;
}
else
{
if (char_byte & 0x80)
v = color;
else
v = 0;
}
//printf("%02X %x \n",char_byte,v);
// Quadruple pixel effect
pixels[screenSurface->w*(2*r)+c*2]=v;
pixels[screenSurface->w*(2*r)+c*2+1]=v;
//pixels[screenSurface->w*(2*r+1)+c*2]=v;
//pixels[screenSurface->w*(2*r+1)+c*2+1]=v;
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
}
}
} // TEXT MODE
else if (((screen_switches & _TEXT_GRAPHICS) == _TEXT_GRAPHICS) && ((screen_switches & _LORES_HIRES) == 0))
{
// LORES MODE
if ((screen_switches & _PAGE1_PAGE2) == 0)
base_address = _LO_RES1;
else
base_address = _LO_RES2;
for (r=0;r<SCREEN_HEIGHT/2;r++)
{
text_r = r / 8;
gr_r = r / 4;
for (c=0;c<screenSurface->w/2;c++)
{
if (((screen_switches & _ALL_MIX) == _ALL_MIX) && (text_r > 19))
{
// MIX MODE
text_c = c / 7;
if ((c % 7) == 0)
{
c_val = RAM[base_address+page_row_memory_map[text_r]+text_c];
mode = c_val >> 6;
// reduce to 5x7 matrix
//c_val = c_val & 0x3F;
char_byte = _CHARROM[c_val*8+(r&0x07)];
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
// Inverse
if (mode == 0)
char_byte = char_byte ^ 0xFF;
// Flashing
if ((mode == 1) && (flash_count >= 0x1f))
char_byte = char_byte ^ 0xFF;
}
if (REVERSE_CHAR_ROM)
{
if (char_byte & 0x01)
v = color;
else
v = 0;
}
else
{
if (char_byte & 0x80)
v = color;
else
v = 0;
}
} // // MIX MODE
else
{
// GRAPHIC SECTION
gr_c = c / 7;
c_val = RAM[base_address+page_row_memory_map[text_r]+gr_c];
if ((gr_r & 0x01) == 1)
v = palette[(c_val & 0xF0)>>4];
else
v = palette[(c_val & 0x0F)];
}
//printf("%02X %x \n",char_byte,v);
// Quadruple pixel effect
pixels[screenSurface->w*(2*r)+c*2]=v;
pixels[screenSurface->w*(2*r)+c*2+1]=v;
//pixels[screenSurface->w*(2*r+1)+c*2]=v;
//pixels[screenSurface->w*(2*r+1)+c*2+1]=v;
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
}
}
} // LORES MODE
else if (((screen_switches & _TEXT_GRAPHICS) == _TEXT_GRAPHICS) && ((screen_switches & _LORES_HIRES) == _LORES_HIRES))
{
// HIRES MODE
if ((screen_switches & _PAGE1_PAGE2) == 0)
{
base_address = _HI_RES1;
base_address_text = _LO_RES1;
}
else
{
base_address = _HI_RES2;
base_address_text = _LO_RES2;
}
for (r=0;r<SCREEN_HEIGHT/2;r++)
{
text_r = r / 8;
gr_r = r % 8;
for (c=0;c<screenSurface->w/2;c++)
{
if (((screen_switches & _ALL_MIX) == _ALL_MIX) && (text_r > 19))
{
// MIX MODE
text_c = c / 7;
if ((c % 7) == 0)
{
c_val = RAM[base_address_text+page_row_memory_map[text_r]+text_c];
mode = c_val >> 6;
// reduce to 5x7 matrix
//c_val = c_val & 0x3F;
char_byte = _CHARROM[c_val*8+(r&0x07)];
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
// Inverse
if (mode == 0)
char_byte = char_byte ^ 0xFF;
// Flashing
if ((mode == 1) && (flash_count >= 0x1f))
char_byte = char_byte ^ 0xFF;
}
if (REVERSE_CHAR_ROM)
{
if (char_byte & 0x01)
v = color;
else
v = 0;
}
else
{
if (char_byte & 0x80)
v = color;
else
v = 0;
}
} // MIX MODE
else
{
// GRAPHIC SECTION
gr_c = c / 7;
if ((c % 7) == 0)
{
c_val = RAM[base_address+page_row_memory_map[text_r]+gr_r*1024+gr_c];
hcolor = (c_val & 0x80) >> 7;
}
p = c_val & 0x01;
if ((c % 7) <= 5)
{
p_next = c_val & 0x02;
}
else
{
p_next = RAM[base_address+page_row_memory_map[text_r]+gr_r*1024+gr_c+1] & 0x01;
}
if (p == 0)
{
v = 0x00000000;
}
else
{
if (p_next)
{
v = 0x00ffffff;
}
else
{
if (c%2)
{
v = hpalette1[hcolor];
}
else
{
v = hpalette0[hcolor];
}
}
}
c_val = c_val >> 1;
}
// Quadruple pixel effect
pixels[screenSurface->w*(2*r)+c*2]=v;
pixels[screenSurface->w*(2*r)+c*2+1]=v;
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
}
}
} // HIRES MODE
SDL_UpdateWindowSurface(window);
}
void video_init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
#ifdef WITHGUI
if(guidebug)
window = SDL_CreateWindow( "PPS-2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT_DBG, SDL_WINDOW_SHOWN );
else
window = SDL_CreateWindow( "PPS-2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
#else
window = SDL_CreateWindow( "PPS-2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
#endif
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface(window);
Main_Renderer = SDL_CreateSoftwareRenderer(screenSurface);
}
}
pixels = (uint32_t *)screenSurface->pixels;
clearscreen();
#ifdef WITHGUI
if(guidebug)
initgui();
#endif
}
void video_test()
{
SDL_Surface* testSurface = NULL;
SDL_Window* testwindow = NULL;
uint32_t *testpixels = NULL;
uint8_t c_val;
uint8_t char_byte;
uint32_t v;
int i,r,c,cr,cc;
//Create window
testwindow = SDL_CreateWindow( "PPS-2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( testwindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
// Get window surface
testSurface = SDL_GetWindowSurface( testwindow );
}
testpixels = (uint32_t *)testSurface->pixels;
// Clear screen
SDL_FillRect(testSurface, NULL, SDL_MapRGB( testSurface->format, 0x00, 0x00, 0x00));
for (i=0;i<256;i++)
{
for (cr=0;cr<8;cr++)
{
r = (i & 0x0f) * 8 +cr;
char_byte = _CHARROM[i*8+cr] & 0x7E;
//printf("%d\n",char_byte);
for (cc=0;cc<8;cc++)
{
c = ((i & 0xf0) >> 1) + cc;
//printf("%d %d %d\n",i,r,c);
if (REVERSE_CHAR_ROM)
{
if (char_byte & 0x01)
v = color;
else
v = 0;
}
else
{
if (char_byte & 0x80)
v = color;
else
v = 0;
}
//printf("%02X %x \n",char_byte,v);
//Quadruple pixel effect
testpixels[testSurface->w*(2*r)+c*2]=v;
testpixels[testSurface->w*(2*r)+c*2+1]=v;
//testpixels[testwindow->w*(2*r+1)+c*2]=v;
//testpixels[testwindow->w*(2*r+1)+c*2+1]=v;
if (REVERSE_CHAR_ROM)
char_byte = char_byte >> 1;
else
char_byte = char_byte << 1;
}
}
}
SDL_UpdateWindowSurface(testwindow);
}