forked from iruka-git/SCMP3Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nybbles.c
319 lines (262 loc) · 5.22 KB
/
nybbles.c
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
/*
* A minimal INS8073 machine
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#ifndef _MSDOS_
#include <termios.h>
#endif
#include <time.h>
#include <unistd.h>
#include <errno.h>
#ifndef _MSDOS_
#include <sys/select.h>
#define TERMINAL_INIT
#endif
#include "ns807x.h"
#define PATCH_ROM 0 // TXTBGN PATCH ( =X'8000 )
#define ROMSIZE 0x1000 // NIBL BASIC ROM SIZE
//#define BASIC_TEXT 0x1400 // NIBL BASIC PROGRAM START
static uint8_t ramrom[65536]; // 64kB RAM
static uint8_t fast;
volatile unsigned done;
static unsigned trace;
struct ns8070 *cpu;
static uint8_t pendc; /* Pending char */
struct timespec pastTime;
void print_vcount();
#ifdef TERMINAL_INIT
unsigned int next_char(void);
int64_t get_cputime();
int check_chario(void)
{
fd_set i, o;
struct timeval tv;
unsigned int r = 0;
FD_ZERO(&i);
FD_SET(0, &i);
FD_ZERO(&o);
FD_SET(1, &o);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(2, &i, &o, NULL, &tv) == -1) {
if (errno == EINTR)
return 0;
perror("select");
exit(1);
}
if (FD_ISSET(0, &i))
r |= 1;
if (FD_ISSET(1, &o))
r |= 2;
return r;
}
void ns8070_emu_chario(void)
{
if (check_chario() & 1) {
pendc = next_char();
/* The TinyBASIC expects break type conditions
that persist for a while - this is a fudge */
if (pendc == 3)
ns8070_set_a(cpu, 0);
else
ns8070_set_a(cpu, 1);
}
}
static struct termios saved_term, term;
static void cleanup(int sig)
{
tcsetattr(0, TCSADRAIN, &saved_term);
done = 1;
}
static void exit_cleanup(void)
{
tcsetattr(0, TCSADRAIN, &saved_term);
int64_t t = get_cputime();
print_vcount(t);
}
void terminal_init()
{
if (tcgetattr(0, &term) == 0) {
saved_term = term;
atexit(exit_cleanup);
signal(SIGINT, cleanup);
signal(SIGQUIT, cleanup);
signal(SIGPIPE, cleanup);
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 1;
term.c_cc[VINTR] = 0;
term.c_cc[VSUSP] = 0;
term.c_cc[VSTOP] = 0;
tcsetattr(0, TCSADRAIN, &term);
}
}
int ns8070_emu_getc(void)
{
int rc;
uint8_t r = pendc;
if (r) {
pendc = 0;
ns8070_set_a(cpu, 1);
rc = write(1, &r, 1);
}
return r;
(void)rc;
}
void ns8070_emu_putc(char r)
{
int rc = write(1, &r, 1);
(void)rc;
}
unsigned int next_char(void)
{
char c;
if (read(0, &c, 1) != 1) {
printf("(tty read without ready byte)\n");
return 0xFF;
}
if (c == 0x0A)
c = '\r';
if (c == 0x1a) { // ^Z
exit(1);
}
if (c == 0x1b) { // ESC
exit(1);
}
if (c == 0x7f) { // ESC
c = 0x08;
}
if( (c>='a')&&(c<='z') ) {c=c-0x20;}
return c;
}
#else
// MSDOS:::
void terminal_init() {}
void ns8070_emu_chario(void) {}
int ns8070_emu_getc(void)
{
char c;
c = getchar();
if (c == 0x0A)
c = '\r';
if (c == 0x1a) { // ^Z
exit(1);
}
if (c == 0x1b) { // ESC
exit(1);
}
if (c == 0x7f) { // ESC
c = 0x08;
}
if( (c>='a')&&(c<='z') ) {c=c-0x20;}
return c;
}
void ns8070_emu_putc(char r)
{
putchar(r);
}
#endif
int64_t get_cputime() {
struct timespec currentTime;
int64_t diffn;
int64_t diffs;
int64_t diffu;
clock_gettime(CLOCK_REALTIME, ¤tTime);
// printf("Current Time: %lu Sec + %lu nanoSec.\n",
// (long)currentTime.tv_sec, currentTime.tv_nsec);
diffn = currentTime.tv_nsec - pastTime.tv_nsec;
diffs = currentTime.tv_sec - pastTime.tv_sec;
diffu = diffn / 1000;
diffu += diffs * 1000 * 1000;
pastTime = currentTime;
return diffu;
}
static void usage(void)
{
fprintf(stderr,
"flexbox: [-f] -r rompath] [-d debug]\n");
exit(EXIT_FAILURE);
}
/****************************************************************************
* メモリー内容をダンプ.
****************************************************************************
*/
void mem_dump(char *msg,int adr,void *ptr,int len)
{
unsigned char *p = (unsigned char *)ptr;
int i;//,j,c;
fprintf(stderr,"%s:\n",msg);
for(i=0; i<len; i++) {
if( (i & 15) == 0 ) fprintf(stderr,"%04x",adr);
if( (i & 15) == 8 ) fprintf(stderr," ");
fprintf(stderr," %02x",*p);
p++;adr++;
if( (i & 15) == 15 ) {
fprintf(stderr,"\n");
}
}
fprintf(stderr,"\n");
}
void dumpreg(struct ns8070 *cpu)
{
mem_dump("rom",0xff00,&cpu->rom[0xffc0],64);
}
int main(int argc, char *argv[])
{
/* static struct timespec tc; */
int opt;
int fd;
int rom = 1;
char *rompath = "nibl3.bin";
/* unsigned int cycles = 0; */
while ((opt = getopt(argc, argv, "d:fi:r:")) != -1) {
switch (opt) {
case 'r':
rompath = optarg;
break;
case 'd':
trace = atoi(optarg);
break;
case 'f':
fast = 1;
break;
default:
usage();
}
}
if (optind < argc)
usage();
if (rom) {
//memset(ramrom,0xff,sizeof(ramrom) );
fd = open(rompath, O_RDONLY);
if (fd == -1) {
perror(rompath);
exit(EXIT_FAILURE);
}
if (read(fd, ramrom, ROMSIZE) != ROMSIZE) {
fprintf(stderr, "nybbles: short rom '%s'.\n",
rompath);
// exit(EXIT_FAILURE);
}
close(fd);
#if PATCH_ROM
// TXTBGN PATCH ( =X'8000 )
ramrom[0x0c]=(BASIC_TEXT/256) ; // BASIC PROGRAM TEXT AREA
#endif
}
/* Patch in our I/O hooks */
cpu = ns8070_create(ramrom);
ns8070_reset(cpu);
ns8070_trace(cpu, trace & TRACE_CPU);
ns8070_set_a(cpu, 1);
ns8070_set_b(cpu, 1);
terminal_init();
get_cputime();
ns8070_executes(cpu);
exit(0);
}