summaryrefslogtreecommitdiffstats
path: root/sdlscrn.c
blob: 2694f2b6d3e082fc44ab91097bfc4809021182e9 (plain)
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
/*
 * sdlscrn.c
 *
 * ABC80 screen emulation (40x24/80x24)
 */

#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "screen.h"
#include "z80.h"

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))

#define TS_WIDTH  80
#define TS_HEIGHT 24

#define FONT_XSIZE 6
#define FONT_YSIZE 10

#define FONT_XDUP  2		/* For 80-column mode */
#define FONT_YDUP  3

#define PX_WIDTH  (TS_WIDTH*FONT_XSIZE*FONT_XDUP)
#define PX_HEIGHT (TS_HEIGHT*FONT_YSIZE*FONT_YDUP)

extern unsigned char abc_font[256][FONT_YSIZE];

#define NCOLORS 2

static uint32_t colors[NCOLORS];

static struct rgba { uint8_t a, r, g, b; } rgbcolors[NCOLORS] = {
  {0x00,0x00,0x00,0x00},	/* black */
  {0x00,0xff,0xff,0xff},	/* white */
};

unsigned char screendata[2048];
static struct { int x, y; } addr_to_xy[2][2048];

static SDL_Surface *rscreen;
static volatile uint8_t blink_mask = 0x80; /* 0x80 for inverse enable */

static int mode40;

/*
 * Get the pointer for a specific row -- this decodes the ABC80
 * funky video memory.
 */
static inline unsigned char *screenptr(int y, int x)
{
  if (mode40)
    return &screendata[1024 + (((y >> 3)*5) << 3) + ((y & 7) << 7) + x];
  else
    return &screendata[(((y >> 3)*5) << 4) + ((y & 7) << 8) + x];
}

/*
 * Prepare screen for modification
 */
static void lock_screen(void)
{
  SDL_LockSurface(rscreen);
}

/*
 * Update the on-screen structure to match the screendata[]
 * for character (tx,ty), but don't refresh the rectangle just
 * yet...
 */
static void put_screen(int tx, int ty)
{
  unsigned char *fontp, v;
  uint32_t *pixelp, *pixelpp, fgp, bgp;
  int x, xx, y, yy;
  int bmask = blink_mask;
  int gx;
  unsigned char gmode;
  unsigned char cc;
  int xdup = FONT_XDUP << mode40;

  gmode = 0;
  for ( gx = 0 ; gx < tx ; gx++ ) {
    cc = *screenptr(ty,gx);
    if ( (cc & 0x68) == 0 )
      gmode = (cc & 0x10) << 3;
  }

  cc = *screenptr(ty,tx);
  fontp = abc_font[(cc & 0x7f) + gmode];

  if ( cc & bmask ) {
    bgp = colors[1];
    fgp = colors[0];
  } else {
    bgp = colors[0];
    fgp = colors[1];
  }
  
  pixelp = ((uint32_t *) rscreen->pixels) +
    ty*PX_WIDTH*FONT_YSIZE*FONT_YDUP + 
    ((tx*FONT_XSIZE*FONT_XDUP) << mode40);
  
  for ( y = 0 ; y < FONT_YSIZE ; y++ ) {
    for ( yy = 0 ; yy < FONT_YDUP ; yy++ ) {
      v = *fontp;
      pixelpp = pixelp;
      for ( x = 0 ; x < FONT_XSIZE ; x++ ) {
	for ( xx = 0 ; xx < xdup ; xx++) {
	  *pixelpp++ = (v & 0x80) ? fgp : bgp;
	}
	v <<= 1;
      }
      pixelp += PX_WIDTH;
    }
    fontp++;
  }
}

/*
 * This routine switches the blink status, then goes around the screen
 * and updates all characters which has the blink attribute set.
 */
static void toggle_blink(void)
{
  int x, y;
  int gx, gy, gw, gh;
  SDL_Rect rects[TS_HEIGHT*TS_WIDTH/2];	/* Absolute maximum needed */
  SDL_Rect *rect = rects-1;
  int nrects = 0;
  int width = TS_WIDTH >> mode40;

  blink_mask ^= 0x80;

  SDL_LockSurface(rscreen);

  gw = (FONT_XSIZE*FONT_XDUP) << mode40;
  gh = FONT_YSIZE*FONT_YDUP;

  for ( y = 0, gy = 0 ; y < TS_HEIGHT ; y++, gy += gh ) {
    for ( x = 0, gx = 0 ; x < width ; x++, gx += gw ) {
      if ( *screenptr(y,x) & 0x80 ) {
	put_screen(x,y);
	if ( !nrects || rect->y != gy || rect->x+rect->w != gx ) {
	  nrects++;
	  rect++;
	  rect->x = gx; rect->y = gy;
	  rect->w = gw; rect->h = gh;
	} else {
	  rect->w += gw;
	}
      }
    }
  }

  SDL_UnlockSurface(rscreen);

  if ( nrects )
    SDL_UpdateRects(rscreen, nrects, rects);
}

/*
 * Refresh rectangle and unlock screen
 * Coordinates are inclusive and must be adjusted for double-pixel mode
 */
static void update_screen(int x0, int y0, int x1, int y1)
{
  SDL_UnlockSurface(rscreen);

  SDL_UpdateRect(rscreen, (x0*FONT_XSIZE*FONT_XDUP) << mode40,
		 y0*FONT_YSIZE*FONT_YDUP,
		 ((x1-x0+1)*FONT_XSIZE*FONT_XDUP) << mode40,
		 (y1-y0+1)*FONT_YSIZE*FONT_YDUP);
}

/*
 * Called whenever something is written to the screen
 */
void
screen_write(int addr, int value)
{
  int x, y, xx;
  int old;
  unsigned char *p;
  int width = TS_WIDTH >> mode40;

  addr = ((addr & 0x800) >> 1) | (addr & 0x3ff);
  
  x = addr_to_xy[mode40][addr].x;
  y = addr_to_xy[mode40][addr].y;
  if ( y == -1 )
    return;			/* Nothing to do */

  lock_screen();

  p = &screendata[addr];
  old = *p;

  *p = value;
  put_screen(x,y);
  xx = x+1;

  if ( (old & 0x68) == 0 || (value & 0x68) == 0 ) {
    /* Graphics control character change */
    for ( ; xx < width ; xx++ )
      put_screen(xx,y);
  }

  update_screen(x,y,xx-1,y);
}

void setmode40(int m)
{
  int x, y, width;

  if (m != mode40) {
    mode40 = !!m;

    width = TS_WIDTH >> mode40;

    lock_screen();

    for (y = 0; y < TS_HEIGHT; y++)
      for (x = 0; x < width; x++)
	put_screen(x, y);

    update_screen(0,0,width-1,23);

    load_basic(m);
  }
}

/*
 * Initialize SDL and the data structures
 */
void screen_init(void)
{
  int window = 1;		/* True = run in a window */
  int debug = 1;		/* False = force clean shutdown */
  int i, j, x, y;

  if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO
		| (debug ? SDL_INIT_NOPARACHUTE : 0)) )
    return;

  atexit(SDL_Quit);

  if ( !(rscreen = SDL_SetVideoMode(PX_WIDTH, PX_HEIGHT, 32,
		SDL_SWSURFACE | (window ? 0 : SDL_FULLSCREEN))) ) {
    return;
  }

  /* No mouse cursor, please */
  if ( !window )
    SDL_ShowCursor(SDL_DISABLE);

  /* Convert colors to preferred machine representation */
  for ( i = 0 ; i < NCOLORS ; i++ ) {
    colors[i] = SDL_MapRGB(rscreen->format,
			   rgbcolors[i].r, rgbcolors[i].g, rgbcolors[i].b);
  }

  /* Initialize reverse mapping table */
  for ( i = 0 ; i < 2 ; i++ ) {
    mode40 = i;

    for ( j = 0 ; j < 2048 ; j++ ) {
      addr_to_xy[i][j].x = -1;
      addr_to_xy[i][j].y = -1;
    }

    for ( y = 0 ; y < TS_HEIGHT ; y++ ) {
      for ( x = 0 ; x < (TS_WIDTH >> i); x++ ) {
	int p = screenptr(y,x)-screendata;
	addr_to_xy[i][p].x = x;
	addr_to_xy[i][p].y = y;
      }
    }
  }

  mode40 = 0;
  
  /* Blink timer */
  SDL_AddTimer(400, post_periodic, (void *)toggle_blink);

  /* Enable keyboard decoding */
  SDL_EnableUNICODE(1);

  /* Load the appropriate BASIC */
  load_basic(mode40);

  return;
}

/*
 * Cleanup
 */
void screen_reset(void)
{
  /* Handled by atexit */
}

/*
 * Flush the screen
 */
void screen_flush(void)
{
  /* Handled elsewhere */
}

/*
 * Handle events
 */
int keyboard_code;		/* Keyboard code exported to PIO */

void check_event(void)
{
  SDL_Event event;
  static int keyboard_scan = -1; /* No key currently down */

  while ( SDL_PollEvent(&event) ) {
    switch ( event.type ) {
    case SDL_KEYDOWN:
      {
	int mysym = -1;

	switch (event.key.keysym.sym) {
	case SDLK_END:
	  if (event.key.keysym.mod & (KMOD_RALT|KMOD_LALT))
	    exit(0);		/* Alt+End = quit */
	  break;

	case SDLK_LEFT:
	  mysym = 8;
	  break;

	case SDLK_RIGHT:
	  mysym = 9;
	  break;

	default:
	  if (event.key.keysym.unicode <= 0x7f) {
	    mysym = event.key.keysym.unicode;
	  } else {
	    switch ( event.key.keysym.unicode ) {
	    case L'¤':
	      mysym = '$';
	      break;
	    case L'É':
	      mysym = '@';
	      break;
	    case L'Å':
	      mysym = ']';
	      break;
	    case L'Ä':
	      mysym = '[';
	      break;
	    case L'Ö':
	      mysym = '\\';
	      break;
	    case L'Ü':
	      mysym = '^';
	      break;
	    case L'é':
	      mysym = '`';
	      break;
	    case L'å':
	      mysym = '}';
	      break;
	    case L'ä':
	      mysym = '{';
	      break;
	    case L'ö':
	      mysym = '|';
	      break;
	    case L'ü':
	      mysym = '~';
	      break;
	    default:
	      break;
	    }
	  }
	}

	if ( mysym >= 0 ) {
	  keyboard_code = mysym | 0x80;
	  keyboard_scan = event.key.keysym.scancode;
	  z80_state.i_vector = 26;
	  z80_state.interrupt = 1;
	  set_in_port(56, keyboard_code);
	}
      }
      break;
    case SDL_KEYUP:
      {
	if ( event.key.keysym.scancode == keyboard_scan )
	  keyboard_code &= ~0x80;

	set_in_port(56, keyboard_code);
      }
      break;
    case SDL_USEREVENT:
      ((void (*)(void)) event.user.data1)();
      event_pending--;
      break;
    case SDL_QUIT:
      exit(1);
      break;
    }
  }
}