-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.c
215 lines (208 loc) · 2.96 KB
/
basic.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
/*
spiffy - ZX spectrum emulator
Copyright Edward Cree, 2010-13
basic.c - BASIC debugging functions
*/
#include "basic.h"
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
const char *toktbl[]={
"RND ", // 0xA5
"INKEY$ ",
"PI ",
"FN ",
"POINT ",
"SCREEN$ ",
"ATTR ",
"AT ",
"TAB ",
"VAL$ ",
"CODE ",
"VAL ",
"LEN ",
"SIN ",
"COS ",
"TAN ",
"ASN ",
"ACS ",
"ATN ",
"LN ",
"EXP ",
"INT ",
"SQR ",
"SGN ",
"ABS ",
"PEEK ",
"IN ",
"USR ",
"STR$ ",
"CHR$ ",
"NOT ",
"BIN ",
"OR ",
"AND ",
"<=",
">=",
"<>",
"LINE ",
"THEN ",
"TO ",
"STEP ",
"DEF FN ",
"CAT ",
"FORMAT ",
"MOVE ",
"ERASE ",
"OPEN #",
"CLOSE #",
"MERGE ",
"VERIFY ",
"BEEP ",
"CIRCLE ",
"INK ",
"PAPER ",
"FLASH ",
"BRIGHT ",
"INVERSE ",
"OVER ",
"OUT ",
"LPRINT ",
"LLIST ",
"STOP ",
"READ ",
"DATA ",
"RESTORE ",
"NEW ",
"BORDER ",
"CONTINUE ",
"DIM ",
"REM ",
"FOR ",
"GO TO ",
"GO SUB ",
"INPUT ",
"LOAD ",
"LIST ",
"LET ",
"PAUSE ",
"NEXT ",
"POKE ",
"PRINT ",
"PLOT ",
"RUN ",
"SAVE ",
"RANDOMIZE ",
"IF ",
"CLS ",
"DRAW ",
"CLEAR ",
"RETURN ",
"COPY ",
NULL
};
const char *baschar(uint8_t c)
{
static char buf[5];
if(c>=0xA5) return(toktbl[c-0xA5]);
if(c>=0x90)
{
snprintf(buf, 5, "[%c]", c-0x4F);
return(buf);
}
switch(c)
{
case 0x60:
return("£");
case 0x7F:
return("©");
case 0x10:
return("[INK]");
case 0x11:
return("[PAPER]");
case 0x12:
return("[FLASH]");
case 0x13:
return("[BRIGHT]");
case 0x14:
return("[INVERSE]");
case 0x15:
return("[OVER]");
case 0x16:
return("[AT]");
case 0x17:
return("[TAB]");
}
if((c>=0x80)||(c<0x20))
{
snprintf(buf, 5, "\\%03o", c);
return(buf);
}
buf[0]=c;
buf[1]=0;
return(buf);
}
double float_decode(const uint8_t *buf)
{
if(!buf[0])
{
if((!buf[1])||(buf[1]==0xFF))
{
if(!buf[4])
{
uint16_t val=buf[2]|(buf[3]<<8);
if(buf[1]) return(val-131072);
return(val);
}
}
}
signed char exponent=buf[0]-128;
unsigned long mantissa=((buf[1]|0x80)<<24)|(buf[2]<<16)|(buf[3]<<8)|buf[4];
bool minus=buf[1]&0x80;
return((minus?-1.0:1.0)*mantissa*exp2(exponent-32));
}
void float_encode(uint8_t *buf, double val)
{
if((fabs(val)<65536)&&(ceil(val)==val))
{
buf[0]=buf[4]=0;
signed int ival=ceil(val);
if(signbit(val))
{
buf[1]=0xFF;
ival+=131072;
}
else
buf[1]=0;
unsigned int uval=ival;
buf[2]=uval&0xff;
buf[3]=(uval>>8)&0xff;
}
else if(isfinite(val))
{
signed char exponent=1+floor(log2(fabs(val)));
double mantissa=rint(fabs(val)*exp2(32-exponent));
if(mantissa>0xffffffff)
{
fprintf(stderr, "float_encode: 6 Number too big, %g\n", val);
return;
}
unsigned long mi=mantissa;
buf[0]=exponent+128;
buf[1]=((mi>>24)&0x7F)|(signbit(val)?0x80:0);
buf[2]=mi>>16;
buf[3]=mi>>8;
buf[4]=mi;
}
else
{
fprintf(stderr, "float_encode: cannot encode non-finite number %g\n", val);
}
}
int compare_bas_line(const void *a, const void *b)
{
uint16_t na=((bas_line *)a)->number, nb=((bas_line *)b)->number;
if(na<nb) return(-1);
if(na>nb) return(1);
return(0);
}