aboutsummaryrefslogtreecommitdiffstats
path: root/sdlscrn.c
blob: 7862ad59f02267370b3ff8007f9653380363fe23 (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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 * sdlscrn.c
 *
 * ABC80/802 screen emulation (40x24/80x24)
 */

#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "screen.h"
#include "screenshot.h"
#include "z80.h"
#include "clock.h"
#include "abcio.h"
#include "nstime.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 const unsigned char abc_font[256][FONT_YSIZE];

static void check_event(void);

#define NCOLORS 8

static struct argb { uint8_t a, r, g, b; } rgbcolors[NCOLORS] = {
  {0x00,0x00,0x00,0x00},	/* black */
  {0x00,0xff,0x00,0x00},	/* red */
  {0x00,0x00,0xff,0x00},	/* green */
  {0x00,0xff,0xff,0x00},	/* yellow */
  {0x00,0x00,0x00,0xff},	/* blue */
  {0x00,0xff,0x00,0xff},	/* purple */
  {0x00,0x00,0xff,0xff},	/* cyan */
  {0x00,0xff,0xff,0xff},	/* white */
};

#define VRAM_SIZE 2048
#define VRAM_MASK (VRAM_SIZE-1)
unsigned char video_ram[VRAM_SIZE];

union crtc {
  uint8_t regs[18];
  struct {
    uint8_t htotal;		/* Horizontal total characters */
    uint8_t hdisp;		/* Horizontal displayed characters */
    uint8_t hsyncpos;		/* Horizontal sync position (char units) */
    uint8_t hsyncwidth;	        /* Horizontal sync width (char units) */
    uint8_t vscantotal;         /* Vertical total (char units) */
    uint8_t vadjust;		/* Vertical adjust scan lines */
    uint8_t vdisplay;		/* Displayed character rows */
    uint8_t vsyncpos;		/* Vertical sync position */
    uint8_t interlace;		/* Interlace mode */
    uint8_t maxscan;		/* Maximum scan line address */
    uint8_t curstart;		/* Cursor start line */
    uint8_t curend;		/* Cursor end line */
    uint8_t starth;		/* High half of start address */
    uint8_t startl;		/* Low half of start address */
    uint8_t curh;		/* High half of cursor address */
    uint8_t curl;		/* Low half of cursor address */
  } r;
};
static union crtc crtc;
static uint8_t crtc_addr;
static uint16_t startaddr, curaddr;
struct xy {
  uint8_t x, y;
};
static struct xy addr_to_xy_tbl[2][2048];

struct do_event {
  void (*func)(void);
};

/* A local abstraction of a drawing surface */
struct surface {
    SDL_Surface *surf;		/* SDL_Surface object */
    Uint32 colors[NCOLORS];
    /* Keep track of what the dirty rectangle is */
    unsigned int upd_x0, upd_y0, upd_x1, upd_y1;
    int lock_count;		/* Lock nesting count */
    uint64_t updated;		/* Time stamp of last update */
};

static struct surface rscreen;
static bool blink_on = true;
static bool mode40;

/*
 * Give the x,y coordinates for a given location in video RAM
 */
static inline struct xy addr_to_xy(const uint8_t *p)
{
  uint16_t addr = p - video_ram;
  addr = (addr - startaddr) & VRAM_MASK;
  return addr_to_xy_tbl[mode40][addr];
}

/*
 * Compute the raw offset for a specific x,y coordinates
 */
static inline unsigned int screenoffs(uint8_t y, uint8_t x)
{
  size_t offs;

  switch (model) {
  case MODEL_ABC80:
    if (mode40)
      offs = 1024 + (((y >> 3)*5) << 3) + ((y & 7) << 7) + x;
    else
      offs = (((y >> 3)*5) << 4) + ((y & 7) << 8) + x;
    break;

  case MODEL_ABC802:
    offs = (y * 80) + (x << mode40);
    break;

  default:
    abort();
  }

  return offs + startaddr;
}

/*
 * Return a specific character
 */
static inline uint8_t screendata(uint8_t y, uint8_t x)
{
  return video_ram[screenoffs(y,x) & VRAM_MASK];
}

/*
 * Prevent/allow screen refresh
 */
static void lock_screen(struct surface *s)
{
  if (!s->lock_count++)
    SDL_LockSurface(s->surf);
}

static void unlock_screen(struct surface *s)
{
  if (s->lock_count > 0)
    SDL_UnlockSurface(s->surf);
  else if (s->lock_count < 0)
    abort();			/* SHOULD NEVER HAPPEN */

  s->lock_count--;
}

/*
 * 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(struct surface *s, unsigned int tx, unsigned int ty, bool blink)
{
  const unsigned char *fontp;
  unsigned int voffs;
  unsigned char v, vv;
  uint32_t *pixelp, *pixelpp, fgp, bgp;
  unsigned int x, xx, y, yy, gx;
  uint32_t curmask;
  unsigned char gmode, fg, bg;
  unsigned char cc, invmask;
  unsigned int xdup = FONT_XDUP << mode40;

  if (tx >= (unsigned int)(TS_WIDTH >> mode40) ||
      ty >= (unsigned int)TS_HEIGHT)
    return;

  bg = 0;			/* XXX: handle NWBG */
  fg = 7;

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

  voffs = screenoffs(ty,tx);
  cc = video_ram[voffs & VRAM_MASK];
  fontp = abc_font[(cc & 0x7f) + gmode];
  invmask = (blink || model != MODEL_ABC80) ? 0x80 : 0;
  invmask = (cc & invmask) ? 7 : 0;
  bg ^= invmask;
  fg ^= invmask;

  bgp = s->colors[bg];
  fgp = s->colors[fg];

  if (tx < s->upd_x0) s->upd_x0 = tx;
  if (tx > s->upd_x1) s->upd_x1 = tx;
  if (ty < s->upd_y0) s->upd_y0 = ty;
  if (ty > s->upd_y1) s->upd_y1 = ty;

  pixelp = ((uint32_t *) s->surf->pixels) +
    ty*PX_WIDTH*FONT_YSIZE*FONT_YDUP +
    ((tx*FONT_XSIZE*FONT_XDUP) << mode40);

  curmask = 0;
  if (unlikely(voffs == curaddr)) {
    if (blink | (crtc.r.curstart & 0x40)) {
      curmask = (~0U << (crtc.r.curstart & 0x1f));
      curmask &= (2U << (crtc.r.curend & 0x1f))-1;
    }
  }

  for ( y = 0 ; y < FONT_YSIZE ; y++ ) {
    vv = *fontp++;
    if (curmask & 1)
      vv = 0x3f;
    curmask >>= 1;
    for ( yy = 0 ; yy < FONT_YDUP ; yy++ ) {
      v = vv;
      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;
    }
  }
}

/*
 * Refresh rectangle and unlock screen
 */
#define MIN_UPDATE_INTERVAL (10000000) /* 10 ms */

static void update_screen(struct surface *s)
{
  uint64_t now;

  if (s->lock_count > 0)
    return;

  if (s->upd_x0 == UINT_MAX)
    return;			/* Screen unchanged */

  now = nstime();

  if (s->surf->flags & SDL_DOUBLEBUF)
    SDL_Flip(s->surf);
  else
    SDL_UpdateRect(s->surf,
		   (s->upd_x0*FONT_XSIZE*FONT_XDUP) << mode40,
		   s->upd_y0*FONT_YSIZE*FONT_YDUP,
		   ((s->upd_x1-s->upd_x0+1)*FONT_XSIZE*FONT_XDUP) << mode40,
		   (s->upd_y1-s->upd_y0+1)*FONT_YSIZE*FONT_YDUP);

  s->updated = now;
  s->upd_x0 = s->upd_y0 = UINT_MAX;
  s->upd_x1 = s->upd_y1 = 0;
}

/* Mark the whole screen dirty */
static void screen_dirty(struct surface *s)
{
  s->upd_x0 = s->upd_y0 = 0;
  s->upd_x1 = (TS_WIDTH-1) >> mode40;
  s->upd_y1 = TS_HEIGHT-1;
}

/* Refresh the entire screen or recreate the screen on another surface */
static void refresh_screen(struct surface *s, bool blink)
{
  unsigned int x, y;
  unsigned int width = TS_WIDTH >> mode40;

  screen_dirty(s);

  lock_screen(s);

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

  unlock_screen(s);
  update_screen(s);
}

/*
 * Called whenever something is written to the screen
 */
void write_screen(uint8_t *p, uint8_t v)
{
  struct xy xy;
  uint8_t oldv;
  int width = TS_WIDTH >> mode40;

  oldv = *p;
  if (v == oldv)
    return;			/* Nothing to do */

  *p = v;

  xy = addr_to_xy(p);
  if ( xy.y >= TS_HEIGHT )
    return;			/* Nothing to do */

  lock_screen(&rscreen);

  put_screen(&rscreen, xy.x, xy.y, blink_on);

  if ( (oldv & 0x68) == 0 || (v & 0x68) == 0 ) {
    /* Graphics control character change */
    for ( ; xy.x < width ; xy.x++ )
      put_screen(&rscreen, xy.x, xy.y, blink_on);
  }

  unlock_screen(&rscreen);
}

static void do_set_mode40(bool m)
{
  mode40 = m;

  refresh_screen(&rscreen, blink_on);
  if (model == MODEL_ABC80)
    abc80_mem_mode40(m);
}

void setmode40(bool m)
{
  if (m != mode40)
    do_set_mode40(m);
}

/*
 * This routine switches the blink status, then goes around the screen
 * and updates all characters which has any kind of blink.  Returns the
 * previous value.
 */
static bool set_blink(bool to_what)
{
  struct xy xy;
  int x, y;
  int width = TS_WIDTH >> mode40;

  if (likely(to_what == blink_on))
    return to_what;

  blink_on = to_what;

  lock_screen(&rscreen);

  switch (model) {
  case MODEL_ABC80:
    for ( y = 0 ; y < TS_HEIGHT ; y++ ) {
      for ( x = 0 ; x < width ; x++ ) {
	if ( screendata(y,x) & 0x80 )
	  put_screen(&rscreen, x, y, blink_on);
      }
    }
    break;

  case MODEL_ABC802:
    if (!(crtc.r.curstart & 0x40)) {
      xy = addr_to_xy(curaddr + video_ram);
      put_screen(&rscreen, xy.x, xy.y, blink_on);
    }
    break;
  }

  unlock_screen(&rscreen);

  return !to_what;		/* We just flipped it... */
}

/* Called from the timer that corresponds to the simulated vsync */
void vsync_screen(void)
{
  const int blink_rate = 400/20; /* 400 ms/20 ms = 2.5 Hz */
  static int blink_ctr;

  check_event();		/* Poll for an SDL event */

  if (!blink_ctr--) {
    set_blink(!blink_on);
    blink_ctr += blink_rate;
  }

  update_screen(&rscreen);
}

/*
 * Wrap an SDL_Surface in our local stuff
 */
static struct surface *init_surface(struct surface *s)
{
  int i;

  if (unlikely(!s || !s->surf))
    return NULL;

  /* The whole surface is dirty right now */
  s->upd_x0 = s->upd_y0 = 0;
  s->upd_x1 = (TS_WIDTH-1) >> mode40;
  s->upd_y1 = TS_HEIGHT-1;

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

  /* Surface is unlocked */
  s->lock_count = 0;

  return s;
}

/*
 * Screenshot setup
 */
static void abc_screenshot(void)
{
  struct surface s;

  s.surf = SDL_CreateRGBSurface(SDL_SWSURFACE, PX_WIDTH, PX_HEIGHT, 32,
				0x00ff0000, 0x0000ff00, 0x000000ff, 0);
  if (!init_surface(&s))
    return;
  refresh_screen(&s, true);	/* Always snapshot with blink on */

  screenshot(s.surf);
  SDL_FreeSurface(s.surf);
}

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

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

  atexit(SDL_Quit);

  rscreen.surf = SDL_SetVideoMode(PX_WIDTH, PX_HEIGHT, 32,
			     SDL_HWSURFACE | SDL_DOUBLEBUF |
			     (window ? 0 : SDL_FULLSCREEN));

  /* No mouse cursor in full screen mode */
  if ( !window )
    SDL_ShowCursor(SDL_DISABLE);

  /* Initialize CRTC values to something sensible (also applies for ABC80) */
  memset(&crtc, 0, sizeof crtc);
  crtc.r.htotal = 80;
  crtc.r.hdisp  = 80;
  crtc.r.vscantotal = 24;
  crtc.r.vdisplay = 24;
  crtc.r.curstart = 0x1f;	/* No CRTC cursor */
  startaddr = curaddr = 0;

  /* Initialize reverse mapping table */
  memset(addr_to_xy_tbl, -1, sizeof addr_to_xy_tbl);
  for ( i = 0 ; i < 2 ; i++ ) {
    mode40 = i;
    for ( y = 0 ; y < TS_HEIGHT ; y++ ) {
      for ( x = 0 ; x < (TS_WIDTH >> i); x++ ) {
	size_t p = screenoffs(y,x);
	addr_to_xy_tbl[i][p].x = x;
	addr_to_xy_tbl[i][p].y = y;
      }
    }
  }

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

  /* Enable keyboard repeat */
  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

  if (!init_surface(&rscreen))
    return;

  /* Forcibly set the screen width and load the appropriate BASIC */
  do_set_mode40(width40);
}

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

/*
 * Handle events.  This is called from vsync_screen(), because
 * SDL_PollEvent() might be expensive on some platforms.
 */
int keyboard_code;		/* Keyboard code exported to PIO/DART */

static void check_event(void)
{
  SDL_Event event;
  static int keyboard_scan = -1; /* No key currently down */
  enum kshift {
    KSH_SHIFT = 1,
    KSH_CTRL  = 2,
    KSH_ALT   = 4
  } kshift;

  while ( SDL_PollEvent(&event) ) {
    switch ( event.type ) {
    case SDL_KEYDOWN:
      kshift = \
	((event.key.keysym.mod & (KMOD_LALT|KMOD_RALT)) ? KSH_ALT : 0) |
	((event.key.keysym.mod & (KMOD_LCTRL|KMOD_RCTRL)) ? KSH_CTRL : 0) |
	((event.key.keysym.mod & (KMOD_LSHIFT|KMOD_RSHIFT)) ? KSH_SHIFT : 0);

      if (kshift & KSH_ALT) {
	/* Alt+key are special functions */

	switch (event.key.keysym.sym) {
	case SDLK_END:
	case SDLK_q:
	  exit(0);

	case SDLK_s:
	  abc_screenshot();
	  break;

	case SDLK_r:
	  z80_reset();
	  break;

	case SDLK_n:
	  z80_nmi();
	  break;

	case SDLK_m:
	  dump_memory(false);
	  break;

	case SDLK_u:
	  dump_memory(true);
	  break;

	default:
	  break;
	}
      } else {
	int mysym = -1;

	switch (event.key.keysym.sym) {
	case SDLK_LEFT:
	  mysym = 8;
	  break;

	case SDLK_RIGHT:
	  mysym = 9;
	  break;

	case SDLK_F1:
	case SDLK_F2:
	case SDLK_F3:
	case SDLK_F4:
	case SDLK_F5:
	case SDLK_F6:
	case SDLK_F7:
	case SDLK_F8:
	  mysym = (event.key.keysym.sym - SDLK_F1 + 192) + ((int)kshift << 3);
	  break;

	case SDLK_ESCAPE:
	  mysym = 127;
	  break;

	case SDLK_SPACE:	/* Ctrl+Space -> NUL */
	  mysym = (kshift^KSH_CTRL) << 4;
	  break;

	default:
	  switch (event.key.keysym.unicode) {
	  case   1: case   2: case   3:
	  case   4: case   5: case   6: case   7:
	  case   8: case   9: case  10: case  11:
	  case  12: case  13: case  14: case  15:
	  case  16: case  17: case  18: case  19:
	  case  20: case  21: case  22: case  23:
	  case  24: case  25: case  26: case  27:
	  case  28: case  29: case  30: case  31:
	  case ' ': case '!': case '"': case '#':
	  case '$': case '%': case '&': case  39:
	  case '(': case ')': case '*': case '+':
	  case ',': case '-': case '.': case '/':
	  case '0': case '1': case '2': case '3':
	  case '4': case '5': case '6': case '7':
	  case '8': case '9': case ':': case ';':
	  case '=': case '?':
	  case '@': case 'A': case 'B': case 'C':
	  case 'D': case 'E': case 'F': case 'G':
	  case 'H': case 'I': case 'J': case 'K':
	  case 'L': case 'M': case 'N': case 'O':
	  case 'P': case 'Q': case 'R': case 'S':
	  case 'T': case 'U': case 'V': case 'W':
	  case 'X': case 'Y': case 'Z': case '[':
	  case  92: case ']': case '^': case '_':
	  case '`': case 'a': case 'b': case 'c':
	  case 'd': case 'e': case 'f': case 'g':
	  case 'h': case 'i': case 'j': case 'k':
	  case 'l': case 'm': case 'n': case 'o':
	  case 'p': case 'q': case 'r': case 's':
	  case 't': case 'u': case 'v': case 'w':
	  case 'x': case 'y': case 'z': case '{':
	  case '|': case '}': case '~': case 127:
	    mysym = event.key.keysym.unicode;
	    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;
	  case L'ü':
	    mysym = '~';
	    break;
	  case L'<':
	  case L'>':
	    mysym = (kshift & KSH_CTRL) ? 127 : event.key.keysym.unicode;
	    break;
	  case L'§':
	  case L'½':
	    mysym = 127;
	    break;
	  default:
	    break;
	  }
	  if (!(mysym & ~0x1f)) {
	    /* Shift+Ctrl -> invert bit 4 */
	    if (kshift == (KSH_CTRL|KSH_SHIFT))
	      mysym ^= 0x10;
	  }
	}
	if ( mysym >= 0 ) {
	  /* Remember which key so we can tell when it is released */
	  keyboard_scan = event.key.keysym.scancode;
	  keyboard_down(mysym);
	}
      }
      break;
    case SDL_KEYUP:
	if ( event.key.keysym.scancode == keyboard_scan )
	  keyboard_up();
      break;
    case SDL_QUIT:
      exit(1);
      break;
    default:
      break;
    }
  }
}

void crtc_out(uint8_t port, uint8_t data)
{
  if (!(port & 1)) {
    crtc_addr = data;
    return;
  }

  if (crtc_addr >= sizeof crtc.regs)
    return;

  crtc.regs[crtc_addr] = data;

  startaddr = ((crtc.r.starth & 0x3f) << 8) + crtc.r.startl;
  curaddr   = ((crtc.r.curh & 0x3f) << 8)   + crtc.r.curl;

  refresh_screen(&rscreen, blink_on);
}

uint8_t crtc_in(uint8_t port)
{
  if (!(port & 1))
    return crtc_addr;

  if (crtc_addr >= sizeof crtc.regs)
    return 0xff;

  return crtc.regs[crtc_addr];
}