-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathios.c
More file actions
613 lines (513 loc) · 16.9 KB
/
ios.c
File metadata and controls
613 lines (513 loc) · 16.9 KB
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
#include <stdio.h>
#include <ncurses.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include "z80.h"
#include "ios.h"
#include "psg.h"
#include "leds.h"
#include "lcd.h"
#include "main.h"
#include "vga.h"
#include "main.h"
#define PORTLEDS 0x00
#define PORTBUTTONS 0x00
#define PORTDISP 0x10
#define PORTDATA 0x50 // Video ports
#define PORTADDRL 0x51
#define PORTADDRH 0x52
#define PORTMODE 0x53
#define PORTTIMER 0x54 // TIMER ENABLE/EOI
#define PORTKEY 0x55 // PS/2 DATA
#define PORTAYADDR 0x56
#define PORTAYDATA 0x57
#define PORTSERSTATUS 0x58
#define PORTSERCTL 0x58
#define PORTSER_EN 2
#define PORTSER_RTSON 1
#define PORTSER_DIS 0
#define PORTSER_RTSOFF 0
#define PORTSERDATA 0x59 // SERIAL DATA
#define PORTSPISTATUS 0x5A
#define PORTSPICTL 0x5A
#define PORTSPIDATA 0x5B
#define PORTFPGASTATUS 0x5F
////////////////////////////////////////////////////////////////////////////////
void default_out_callback (ios_t *ios, uint8_t port, uint8_t value){}
////////////////////////////////////////////////////////////////////////////////
void new_out_callback (ios_t *ios, uint8_t port, uint8_t value){
main_data_t *maindata = ios->maindata;
switch (port){
case PORTLEDS:
leds_out(maindata->panel->leds,value);
break;
case PORTDISP:
lcd_out(maindata->panel->lcd, value);
break;
case PORTSERDATA:
if (value != 0x0d){
addch(value);
refresh();
}
break;
case PORTSPICTL:
case PORTSPIDATA:
ch376_out(ios->ch376, port, value);
break;
case PORTDATA:
case PORTADDRL:
case PORTADDRH:
case PORTMODE:
vga_out(maindata->vga, port, value, maindata->sdl->wminimized_main);
break;
case PORTTIMER:
ios->porttimer = value;
break;
case PORTSERCTL:
ios->portserctl = value;
break;
case PORTAYADDR:
ios->psgaddr = value;
break;
case PORTAYDATA:
psg_outreg(ios->psg, ios->psgaddr, value);
break;
}
}
////////////////////////////////////////////////////////////////////////////////
uint8_t default_in_callback (ios_t *ios, uint8_t port){
return 0xff;
}
pthread_mutex_t ios_mutex = PTHREAD_MUTEX_INITIALIZER;
////////////////////////////////////////////////////////////////////////////////
uint8_t new_in_callback (ios_t *ios, uint8_t port){
main_data_t *maindata = ios->maindata;
switch (port){
case PORTDATA:
return vga_in(maindata->vga, port);
case PORTBUTTONS:
return ios->buttons_state;
case PORTSERSTATUS:
return 0;
case PORTSPISTATUS:
case PORTSPIDATA:
return ch376_in(ios->ch376, port);
case PORTFPGASTATUS:
return ios->fpga_status;
case PORTKEY:
pthread_mutex_lock(&ios_mutex);
uint8_t val = 0xff;
if (ios->ps2_qty){
val = ios->ps2_queue[ios->ps2_tail++];
if (ios->ps2_tail == sizeof(ios->ps2_queue))
ios->ps2_tail = 0;
ios->ps2_qty--;
}
if (!ios->ps2_qty)
ios->fpga_status &= ~0x01;
pthread_mutex_unlock(&ios_mutex);
return val;
case PORTTIMER:
ios->fpga_status &= ~0x02;
return 0xFF;
case PORTSERDATA:
pthread_mutex_lock(&ios_mutex);
ios->fpga_status &= ~0x04;
if (ios->portserdata == 0x0a)
ios->portserdata = 0x0d;
pthread_mutex_unlock(&ios_mutex);
return ios->portserdata;
}
return 0xff;
}
////////////////////////////////////////////////////////////////////////////////
void default_hw_run(ios_t *ios){}
////////////////////////////////////////////////////////////////////////////////
void *thread_timer(void *arg){
ios_t *ios = arg;
time_t start = time(NULL);
time_t now;
int ticks = 0;
for (;!ios->endthreads;){
now = time(NULL);
int deltaticks = 300 * (now - start);
if (labs(deltaticks) > (300*300)){
start = now;
ticks = deltaticks = 0;
}
int adjust = 0;
int delta2 = ticks - deltaticks;
if (delta2 < -200)
adjust = -133;
else
if (delta2 < -100)
adjust = -33;
else
if (delta2 > 200)
adjust = 133;
else
if (delta2 > 100)
adjust = 33;
usleep(3333+adjust); // 300Hz
ticks++;
if (ios->porttimer & 0x01)
ios->fpga_status |= 0x02;
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void *thread_serial(void *arg){
ios_t *ios = arg;
fd_set readfds;
struct timeval tv;
for (;!ios->endthreads;){
if ( ( (ios->portserctl & (PORTSER_EN|PORTSER_RTSON)) == (PORTSER_EN|PORTSER_RTSON) )
&& (!(ios->fpga_status & 0x04) ) ){
FD_ZERO (&readfds);
FD_SET (0,&readfds);
tv.tv_sec = 0;
tv.tv_usec = 10;
select (1,&readfds,NULL,NULL,&tv);
if (FD_ISSET(0,&readfds)) {
pthread_mutex_lock(&ios_mutex);
ios->portserdata = getch();
ios->fpga_status |= 0x04;
pthread_mutex_unlock(&ios_mutex);
}
}
else
usleep(10);
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void *thread_psg(void *arg){
ios_t *ios = arg;
for (;!ios->endthreads;){
psg_run(ios->psg);
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void ps2_insert(ios_t *ios, uint8_t code){
pthread_mutex_lock(&ios_mutex);
if (ios->ps2_qty < sizeof(ios->ps2_queue)){
ios->ps2_queue[ios->ps2_head++] = code;
if (ios->ps2_head == sizeof(ios->ps2_queue))
ios->ps2_head = 0;
ios->fpga_status |= 0x01;
ios->ps2_qty++;
}
pthread_mutex_unlock(&ios_mutex);
}
////////////////////////////////////////////////////////////////////////////////
typedef struct {
int keycode;
uint8_t scancode1;
uint8_t scancode2;
} kcode_t;
const kcode_t kcodes[] = {
{'0',0x45,0x00},
{'1',0x16,0x00},
{'2',0x1e,0x00},
{'3',0x26,0x00},
{'4',0x25,0x00},
{'5',0x2e,0x00},
{'6',0x36,0x00},
{'7',0x3d,0x00},
{'8',0x3e,0x00},
{'9',0x46,0x00},
{'a',0x1c,0x00},
{'b',0x32,0x00},
{'c',0x21,0x00},
{'d',0x23,0x00},
{'e',0x24,0x00},
{'f',0x2b,0x00},
{'g',0x34,0x00},
{'h',0x33,0x00},
{'i',0x43,0x00},
{'j',0x3b,0x00},
{'k',0x42,0x00},
{'l',0x4b,0x00},
{'m',0x3a,0x00},
{'n',0x31,0x00},
{'o',0x44,0x00},
{'p',0x4D,0x00},
{'q',0x15,0x00},
{'r',0x2d,0x00},
{'s',0x1b,0x00},
{'t',0x2c,0x00},
{'u',0x3c,0x00},
{'v',0x2a,0x00},
{'w',0x1d,0x00},
{'x',0x22,0x00},
{'y',0x35,0x00},
{'z',0x1a,0x00},
{'-',0x4e,0x00},
{'=',0x55,0x00},
{',',0x41,0x00},
{'.',0x49,0x00},
{'/',0x51,0x00},
{'[',0x5b,0x00},
{']',0x5d,0x00},
{'\\',0x61,0x00},
{';',0x4a,0x00},
{'`',0x54,0x00},
{0x27,0x0e,0x00},
{'~',0x52,0x00},
{SDLK_BACKSPACE,0x66,0x00},
{SDLK_TAB,0x0d,0x00},
{SDLK_RETURN,0x5a,0x00},
{SDLK_ESCAPE,0x76,0x00},
{SDLK_SPACE,0x29,0x00},
{SDLK_RIGHT,0xe0,0x74},
{SDLK_LEFT,0xe0,0x6b},
{SDLK_DOWN,0xe0,0x72},
{SDLK_UP,0xe0,0x75},
{SDLK_CAPSLOCK,0x58,0x00},
{SDLK_LSHIFT,0x12,0x00},
{SDLK_RSHIFT,0x59,0x00},
{SDLK_LCTRL,0x14,0x00},
{SDLK_RCTRL,0xe0,0x14},
{SDLK_INSERT,0xe0,0x70},
{SDLK_DELETE,0xe0,0x71},
{SDLK_HOME,0xe0,0x6c},
{SDLK_END,0xe0,0x69},
{SDLK_PAGEUP,0xe0,0x7d},
{SDLK_PAGEDOWN,0xe0,0x7a},
{0x00,0x00,0x00}
};
////////////////////////////////////////////////////////////////////////////////
const kcode_t *find_kcode(int keycode){
const kcode_t *k = kcodes;
for (;k->keycode;){
if (k->keycode == keycode) return k;
k++;
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void proc_keydown(ios_t *ios, int asccode){
main_data_t *maindata = ios->maindata;
switch(asccode){
case SDLK_F1:
ios->buttons_state &= ~0b00000001;
break;
case SDLK_F2:
ios->buttons_state &= ~0b00000010;
break;
case SDLK_F3:
ios->buttons_state &= ~0b00000100;
break;
case SDLK_F4:
ios->buttons_state &= ~0b00001000;
break;
case SDLK_F5:
ios->buttons_state &= ~0b00010000;
break;
case SDLK_F6:
ios->buttons_state &= ~0b00100000;
break;
case SDLK_F7:
ios->buttons_state &= ~0b01000000;
break;
case SDLK_F8:
ios->buttons_state &= ~0b10000000;
break;
case SDLK_F9:
ios->buttons_state &= ~0b100000000; // Reset
z80_reset(&maindata->z);
leds_reset(maindata->panel->leds);
psg_reset(ios->psg);
vga_reset(maindata->vga);
break;
case SDLK_F12:
z80_break(&maindata->z);
break;
default:
const kcode_t *k = find_kcode(asccode);
if (k){
ps2_insert(ios, k->scancode1);
if (k->scancode2)
ps2_insert(ios, k->scancode2);
}
break;
}
}
////////////////////////////////////////////////////////////////////////////////
void proc_keyup(ios_t *ios, int asccode){
switch(asccode){
case SDLK_F1:
ios->buttons_state |= 0b00000001;
break;
case SDLK_F2:
ios->buttons_state |= 0b00000010;
break;
case SDLK_F3:
ios->buttons_state |= 0b00000100;
break;
case SDLK_F4:
ios->buttons_state |= 0b00001000;
break;
case SDLK_F5:
ios->buttons_state |= 0b00010000;
break;
case SDLK_F6:
ios->buttons_state |= 0b00100000;
break;
case SDLK_F7:
ios->buttons_state |= 0b01000000;
break;
case SDLK_F8:
ios->buttons_state |= 0b10000000;
break;
case SDLK_F9:
ios->buttons_state |= 0b100000000; // Reset
break;
default:
const kcode_t *k = find_kcode(asccode);
if (k){
if (!k->scancode2){
ps2_insert(ios, 0xF0);
ps2_insert(ios, k->scancode1);
}
else{
ps2_insert(ios, k->scancode1);
ps2_insert(ios, 0xF0);
ps2_insert(ios, k->scancode2);
}
}
break;
}
}
////////////////////////////////////////////////////////////////////////////////
void *thread_sdl_events(void *arg){
ios_t *ios = arg;
main_data_t *maindata = ios->maindata;
SDL_Event event;
for (;!ios->endthreads;){
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT){
Uint32 windowID = event.window.windowID;
SDL_Window* affectedWindow = SDL_GetWindowFromID(windowID); // Get the window pointer
if ((event.window.event == SDL_WINDOWEVENT_RESTORED)
||
(event.window.event == SDL_WINDOWEVENT_MOVED)){
if (affectedWindow == maindata->sdl->window_main){
maindata->sdl->wminimized_main = 0;
maindata->sdl->repaint_window_main = 1;
}
else{
maindata->panel->wminimized_panel = 0;
maindata->panel->repaint_window_panel = 1;
}
}
else if (event.window.event == SDL_WINDOWEVENT_MINIMIZED){
if (affectedWindow == maindata->sdl->window_main)
maindata->sdl->wminimized_main = 1;
else
maindata->panel->wminimized_panel = 1;
}
else if (event.window.event == SDL_WINDOWEVENT_CLOSE){
Uint32 windowID = event.window.windowID;
SDL_Window* affectedWindow = SDL_GetWindowFromID(windowID); // Get the window pointer
if (affectedWindow == maindata->sdl->window_main){
z80_break(&maindata->z);
}
else
SDL_HideWindow(affectedWindow);
}
}
else if (event.type == SDL_MOUSEBUTTONDOWN) {
Uint32 windowID = event.window.windowID;
SDL_Window* affectedWindow = SDL_GetWindowFromID(windowID); // Get the window pointer
if (affectedWindow == maindata->sdl->window_main){
SDL_ShowWindow(maindata->panel->window_panel);
maindata->panel->wminimized_panel = 0;
maindata->panel->repaint_window_panel = 1;
}
}
// else if (event.type == SDL_QUIT) {
//
// addstr("QUIT!"); refresh();
// // Handle quit event
// //Uint32 windowID = event.window.windowID;
// //SDL_Window* affectedWindow = SDL_GetWindowFromID(windowID); // Get the window pointer
// //if (affectedWindow == maindata->sdl->window_main){
// z80_break(&maindata->z);
// //}
// //else
// // SDL_HideWindow(affectedWindow);
// }
else if (event.type == SDL_KEYDOWN) {
// char buf[80];
// sprintf(buf,"Event:%02x Scancode:%02x ",event.key.keysym.sym, event.key.keysym.scancode);
// addstr(buf); refresh();
if (event.key.keysym.sym == 0x40000000){
if (event.key.keysym.scancode == 0x34)
event.key.keysym.sym = '~';
if (event.key.keysym.scancode == 0x2f)
event.key.keysym.sym = '`';
}
proc_keydown(ios, event.key.keysym.sym);
buttons_update(maindata->panel->buttons, ios->buttons_state);
} else if (event.type == SDL_KEYUP) {
proc_keyup(ios, event.key.keysym.sym);
buttons_update(maindata->panel->buttons, ios->buttons_state);
}
}
usleep(10000);
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void new_hw_run(ios_t *ios){
if (!ios->presc){
usleep(20);
ios->presc = 50;
}
else
--ios->presc;
}
////////////////////////////////////////////////////////////////////////////////
int default_irq_sample(ios_t *ios){
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int new_irq_sample(ios_t *ios){
return ios->fpga_status & 0x07;
}
////////////////////////////////////////////////////////////////////////////////
ios_t *ios_init(void *main, char *disk_filename){
ios_t *ios = malloc(sizeof(ios_t));
if (!ios) return NULL;
ios->maindata = main;
ios->fpga_status = 0;
ios->porttimer = 0;
ios->portserdata = 0;
ios->portserctl = 0;
ios->psgaddr = 0;
ios->buttons_state = 0x1ff;
ios->endthreads = 0;
ios->presc = 0;
ios->psg = psg_init();
ios->ch376 = ch376_init(disk_filename);
ios->ps2_head = ios->ps2_tail = ios->ps2_qty = 0;
pthread_create(&ios->serialthread, NULL, thread_serial, ios);
pthread_create(&ios->timerthread, NULL, thread_timer, ios);
pthread_create(&ios->psgthread, NULL, thread_psg, ios);
pthread_create(&ios->sdleventthread, NULL, thread_sdl_events, ios);
return ios;
}
////////////////////////////////////////////////////////////////////////////////
void ios_close(ios_t *ios){
ios->endthreads = 1;
pthread_join(ios->serialthread, NULL);
pthread_join(ios->timerthread, NULL);
pthread_join(ios->psgthread, NULL);
pthread_join(ios->sdleventthread, NULL);
psg_end(ios->psg);
ch376_end(ios->ch376);
free(ios);
}