Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25c8286cf2 | ||
|
|
79aef8ed38 | ||
|
|
fd876fd44d | ||
|
|
2dafda5fe5 |
No files matched your search
@@ -3,7 +3,7 @@
|
||||
A complete home computer on a Digilent Basys 3 FPGA board, built around a
|
||||
RISC-V processor written from scratch in Verilog:
|
||||
|
||||
- **RV32IMF processor at 90 MHz**: integer, multiply/divide and IEEE 754
|
||||
- **RV32IMF processor at 100 MHz**: integer, multiply/divide and IEEE 754
|
||||
single-precision floating point in hardware, with traps, interrupts, hardware
|
||||
breakpoints and single step.
|
||||
- **VT220 terminal** with ANSI colour on 1280×1024 VGA: 132 × 64 text, or 80 × 32
|
||||
@@ -14,7 +14,10 @@ RISC-V processor written from scratch in Verilog:
|
||||
- **USB keyboard** on the board's USB host port, with a keys-held port for
|
||||
games.
|
||||
- **Files** saved in the board's configuration flash.
|
||||
- **Software**: Dartmouth-style BASIC with graphics, sound and files, a
|
||||
- **Software**: Dartmouth-style BASIC with graphics, sound and files,
|
||||
`DO … LOOP`, block `IF … ELSE … END IF` and `SELECT CASE`, and scientific
|
||||
functions (inverse and hyperbolic trigonometry in radians or degrees,
|
||||
`LOG10`, `ROUND`, `MOD`, `FACT` …); a
|
||||
machine-code monitor with a RISC-V disassembler and assembler, and five
|
||||
original games.
|
||||
|
||||
@@ -119,8 +122,8 @@ store breakpoints, and a resumable NMI (after the Smrnmi extension: `mnepc`,
|
||||
It is a multi-cycle design with the next instruction fetched while the current
|
||||
one decodes: 2 clocks for most instructions, 3 for taken branches, jumps, loads
|
||||
and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5.
|
||||
Floating-point add, subtract, multiply and the fused multiply-adds take 12
|
||||
clocks, divide and square root 36. It runs at 90 MHz and takes about 5,300
|
||||
Floating-point add, subtract, multiply and the fused multiply-adds take 13
|
||||
clocks, divide and square root 37. It runs at 100 MHz and takes about 5,300
|
||||
LUTs (3,000 of them the FPU), 1,900 flip-flops and 6 DSP slices.
|
||||
|
||||
## The machine (`rtl/rv32_system.v`)
|
||||
@@ -152,8 +155,12 @@ uses 49.5 of the XC7A35T's 50.
|
||||
All in C (plus a little assembly), compiled with GCC:
|
||||
|
||||
- `basic/` — BASIC, with single-precision arithmetic on the FPU (`rv32imf`,
|
||||
`ilp32f`), correctly rounded. `lib/` has the few C library functions it
|
||||
needs (small string functions, `setjmp`, and `sinf` … `sqrtf`), since
|
||||
`ilp32f`), correctly rounded, and the structured statements of later
|
||||
Dartmouth BASIC and True BASIC (`DO … LOOP`, `EXIT`, block `IF`, `SELECT
|
||||
CASE`). `lib/` has the few C library functions it
|
||||
needs (small string functions, `setjmp`, and `sinf` … `sqrtf`, `asinf`,
|
||||
`sinhf`, `atanhf`, `log10f`, an exact `fmodf` and the rest, each within 3
|
||||
ulp), since
|
||||
newlib's bring in its stdio and malloc.
|
||||
- `monitor/` — the machine-code monitor: memory, registers (integer and
|
||||
floating point), step, breakpoints, a RISC-V disassembler and a two-pass
|
||||
@@ -163,10 +170,10 @@ All in C (plus a little assembly), compiled with GCC:
|
||||
|
||||
| BASIC on the board | |
|
||||
|---|---|
|
||||
| `Y = SIN(X)` | 37 µs |
|
||||
| `Y = EXP(X)` | 45 µs |
|
||||
| `X = I * 1.5` | 26 µs |
|
||||
| An empty `FOR` loop, each time round | 8.3 µs |
|
||||
| `Y = SIN(X)` | 33 µs |
|
||||
| `Y = EXP(1)` | 33 µs |
|
||||
| `X = I * 1.5` | 23 µs |
|
||||
| An empty `FOR` loop, each time round | 6.4 µs |
|
||||
|
||||
## Building
|
||||
|
||||
@@ -193,7 +200,7 @@ the configuration flash.
|
||||
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32uf`, `rv32mi`, fetched into `third_party/`) and `tests/` (interrupts, the NMI and single step) on `rv32_cpu.v` in xsim, with random wait states on the data bus. Two riscv-tests don't apply: `ma_data` expects misaligned loads and stores to work (here they trap, as the spec allows) and `pmpaddr` needs PMP. |
|
||||
| `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, both graphics modes, sprites, scrolling, flash files, the monitor (step, breakpoint, Pause, memory files, the assembler, Intel HEX), 3 Mbaud. `scripts/sim.sh quick` skips the terminal and the monitor; `wide` checks the 132 × 64 start-up. |
|
||||
| `monitor/test-disasm.py`, `test-asm.py` | the disassembler against GNU objdump on thousands of random instructions, and the assembler against GNU as |
|
||||
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays and graphics modes, and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
|
||||
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays, block structures, graphics modes and maths functions (bit for bit against Python's `math`), and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
|
||||
| `bench/` | the processor alone with 64 KB of RAM, for timing |
|
||||
|
||||
## Toolchain
|
||||
|
||||
Binary file not shown.
+349
-33
@@ -180,6 +180,7 @@ static struct chan ch[NCHAN];
|
||||
static struct chan *chan_arg(u8 mode);
|
||||
static u8 chan_line(struct chan *c);
|
||||
|
||||
static u8 deg; /* OPTION ANGLE DEGREES */
|
||||
static u8 ghi; /* 640x480 (SCREEN 1) */
|
||||
static u16 gw = 320, gh = 240; /* the graphics' size */
|
||||
/* follow the hardware's mode: a game run with CALL may have ended SCREEN 1 */
|
||||
@@ -193,8 +194,13 @@ enum {
|
||||
T_BYE, T_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK, T_SCREEN,
|
||||
T_SAVE, T_LOAD, T_DIR, T_DEL, T_DATE, T_OPEN, T_CLOSE, T_MONITOR, T_CALL, T_BSAVE, T_BLOAD, T_OUTPUT, T_APPEND, T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS,
|
||||
T_SQR, T_INT, T_RND, T_SGN, T_FN, T_TAB, T_PEEK, T_INP, T_LEN, T_VAL, T_ASC, T_EOF,
|
||||
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM, T_LAST
|
||||
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM,
|
||||
T_DO, T_LOOP, T_WHILE, T_UNTIL, T_EXIT, T_ELSE, T_SELECT, T_CASE,
|
||||
T_OPTION, T_PI, T_ANGLE, T_DEG, T_RAD, T_ASIN, T_ACOS, T_SEC, T_CSC, T_COT,
|
||||
T_SINH, T_COSH, T_TANH, T_ASINH, T_ACOSH, T_ATANH, T_LOG10, T_LOG2, T_CEIL,
|
||||
T_IP, T_FP, T_ROUND, T_TRUNCATE, T_MOD, T_MIN, T_MAX, T_FACT, T_LAST
|
||||
};
|
||||
_Static_assert(T_LAST <= 0x100, "too many keywords for a byte");
|
||||
|
||||
static const char *const kw[] = {
|
||||
"LET", "PRINT", "INPUT", "IF", "THEN", "GOTO", "GOSUB", "RETURN", "FOR",
|
||||
@@ -203,7 +209,11 @@ static const char *const kw[] = {
|
||||
"BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK", "SCREEN",
|
||||
"SAVE", "LOAD", "DIR", "DEL", "DATE", "OPEN", "CLOSE", "MONITOR", "CALL", "BSAVE", "BLOAD", "OUTPUT", "APPEND", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
|
||||
"SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC", "EOF",
|
||||
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM"
|
||||
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM",
|
||||
"DO", "LOOP", "WHILE", "UNTIL", "EXIT", "ELSE", "SELECT", "CASE",
|
||||
"OPTION", "PI", "ANGLE", "DEG", "RAD", "ASIN", "ACOS", "SEC", "CSC", "COT",
|
||||
"SINH", "COSH", "TANH", "ASINH", "ACOSH", "ATANH", "LOG10", "LOG2", "CEIL",
|
||||
"IP", "FP", "ROUND", "TRUNCATE", "MOD", "MIN", "MAX", "FACT"
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------- memory */
|
||||
@@ -212,6 +222,7 @@ static const char *const kw[] = {
|
||||
#define NVARS (26 * 11)
|
||||
#define FORMAX 16
|
||||
#define GOSUBMAX 32
|
||||
#define DOMAX 16
|
||||
|
||||
#ifdef __riscv
|
||||
/* at 8000h (basic.ld), where machine-code programs know to load into it */
|
||||
@@ -239,6 +250,10 @@ struct subent { u8 *tp, *ln; };
|
||||
static struct subent subs[GOSUBMAX];
|
||||
static u8 nsub;
|
||||
|
||||
struct doent { u8 *tp, *ln; }; /* tp is the DO itself */
|
||||
static struct doent dos[DOMAX];
|
||||
static u8 ndo;
|
||||
|
||||
static u8 *tp; /* text pointer */
|
||||
static u8 *curln; /* line being run, 0 in direct mode */
|
||||
static u8 *datap; /* next DATA item, 0 = search from the start */
|
||||
@@ -285,7 +300,8 @@ static void put_u16(u16 v)
|
||||
enum {
|
||||
E_SYNTAX, E_LINE, E_RETURN, E_NEXT, E_DATA, E_SUBSCRIPT, E_DIV0, E_MEMORY,
|
||||
E_TYPE, E_STRING, E_FUNC, E_UNDEF_FN, E_FOR, E_GOSUB, E_NOFILE, E_DISKFULL,
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_OPEN, E_NOTOPEN, E_EOF, E_BREAK, E_STOP
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_OPEN, E_NOTOPEN, E_EOF, E_NOLOOP,
|
||||
E_NODO, E_DODEEP, E_NONEXT, E_ENDIF, E_ENDSEL, E_BREAK, E_STOP
|
||||
};
|
||||
static const char *const errmsg[] = {
|
||||
"SYNTAX ERROR", "UNDEFINED LINE NUMBER", "RETURN WITHOUT GOSUB",
|
||||
@@ -294,6 +310,8 @@ static const char *const errmsg[] = {
|
||||
"UNDEFINED FUNCTION", "FOR NESTED TOO DEEPLY", "GOSUB NESTED TOO DEEPLY",
|
||||
"FILE NOT FOUND", "DISK FULL", "TOO MANY FILES", "BAD FILE NAME", "FLASH ERROR",
|
||||
"FILE TOO BIG", "FILE ALREADY OPEN", "FILE NOT OPEN", "INPUT PAST END",
|
||||
"DO WITHOUT LOOP", "LOOP WITHOUT DO", "DO NESTED TOO DEEPLY", "FOR WITHOUT NEXT",
|
||||
"IF WITHOUT END IF", "SELECT WITHOUT END SELECT",
|
||||
"BREAK", "STOP"
|
||||
};
|
||||
|
||||
@@ -576,8 +594,9 @@ static void clear_vars(void)
|
||||
memset(arrays, 0, sizeof arrays);
|
||||
memset(fns, 0, sizeof fns);
|
||||
arr_low = arena + ARENA;
|
||||
nfor = nsub = 0;
|
||||
nfor = nsub = ndo = 0;
|
||||
datap = 0;
|
||||
deg = 0;
|
||||
}
|
||||
|
||||
static void new_prog(void)
|
||||
@@ -633,7 +652,7 @@ static void expect(u8 c)
|
||||
static u8 at_end(void)
|
||||
{
|
||||
u8 c = peekc();
|
||||
return c == 0 || c == ':';
|
||||
return c == 0 || c == ':' || c == T_ELSE; /* ELSE ends IF ... THEN's statement */
|
||||
}
|
||||
|
||||
static u16 get_lineno(void)
|
||||
@@ -759,6 +778,106 @@ static float rnd(void)
|
||||
return (float)((seed >> 8) & 0xFFFFFF) / 16777216.0f;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- scientific functions */
|
||||
#define PI_F 3.14159265358979f
|
||||
|
||||
/* an angle from an inverse function, in the units OPTION ANGLE chose */
|
||||
static float ang(float r) { return deg ? r * 180.0f / PI_F : r; }
|
||||
|
||||
/* sine (c = 0) or cosine (c = 1). In degrees, the angle is reduced exactly
|
||||
to within 45 of a multiple of 90 first, so SIN(180) and COS(90) are 0. */
|
||||
static float sin_cos(float v, u8 c)
|
||||
{
|
||||
float r, y;
|
||||
int k;
|
||||
if (!deg) return c ? cosf(v) : sinf(v);
|
||||
r = fmodf(v, 360.0f);
|
||||
k = (int)floorf(r / 90.0f + 0.5f);
|
||||
r = (r - 90.0f * (float)k) * (PI_F / 180.0f);
|
||||
k += c; /* cos x = sin(x + 90) */
|
||||
y = k & 1 ? cosf(r) : sinf(r);
|
||||
return (k & 2 ? -y : y) + 0.0f; /* + 0: no -0 */
|
||||
}
|
||||
|
||||
/* x rounded (or truncated) to n decimal places, n < 0 to the left of the
|
||||
point: INT(x * 10^n + .5) / 10^n, as in True BASIC */
|
||||
static float round_to(float x, i16 n, u8 rnd)
|
||||
{
|
||||
float p = 1, s;
|
||||
i16 i;
|
||||
if (n < -38 || n > 38) error(E_FUNC);
|
||||
for (i = n < 0 ? -n : n; i; i--) p *= 10.0f;
|
||||
s = n < 0 ? x / p : x * p;
|
||||
if (s >= 8388608.0f || s <= -8388608.0f) return x; /* nothing to round */
|
||||
s = rnd ? floorf(s + 0.5f) : s < 0 ? -floorf(-s) : floorf(s);
|
||||
return (n < 0 ? s * p : s / p) + 0.0f;
|
||||
}
|
||||
|
||||
static float func2(u8 t, float v)
|
||||
{
|
||||
float w;
|
||||
i16 n = 0;
|
||||
u8 i;
|
||||
switch (t) {
|
||||
case T_ANGLE: /* ANGLE(x, y): the direction of (x, y) */
|
||||
expect(',');
|
||||
w = expr();
|
||||
expect(')');
|
||||
if (v == 0 && w == 0) error(E_FUNC);
|
||||
return ang(atan2f(w, v));
|
||||
case T_ROUND: case T_TRUNCATE:
|
||||
if (t == T_TRUNCATE || peekc() == ',') { expect(','); n = to_int(expr()); }
|
||||
expect(')');
|
||||
return round_to(v, n, t == T_ROUND);
|
||||
case T_MOD: /* the sign of the divisor, like INT */
|
||||
expect(',');
|
||||
w = expr();
|
||||
expect(')');
|
||||
if (w == 0) error(E_DIV0);
|
||||
v = fmodf(v, w);
|
||||
if (v != 0 && (v < 0) != (w < 0)) v += w;
|
||||
return v + 0.0f;
|
||||
case T_MIN: case T_MAX:
|
||||
while (accept(',')) {
|
||||
w = expr();
|
||||
if (t == T_MIN ? w < v : w > v) v = w;
|
||||
}
|
||||
expect(')');
|
||||
return v;
|
||||
}
|
||||
expect(')');
|
||||
switch (t) {
|
||||
case T_DEG: return v * 180.0f / PI_F;
|
||||
case T_RAD: return v * PI_F / 180.0f;
|
||||
case T_ASIN: if (v < -1 || v > 1) error(E_FUNC); return ang(asinf(v));
|
||||
case T_ACOS: if (v < -1 || v > 1) error(E_FUNC); return ang(acosf(v));
|
||||
case T_SEC: w = sin_cos(v, 1); if (w == 0) error(E_FUNC); return 1 / w;
|
||||
case T_CSC: w = sin_cos(v, 0); if (w == 0) error(E_FUNC); return 1 / w;
|
||||
case T_COT:
|
||||
if (!deg) { w = tanf(v); if (w == 0) error(E_FUNC); return 1 / w; }
|
||||
w = sin_cos(v, 0);
|
||||
if (w == 0) error(E_FUNC);
|
||||
return sin_cos(v, 1) / w;
|
||||
case T_SINH: return sinhf(v);
|
||||
case T_COSH: return coshf(v);
|
||||
case T_TANH: return tanhf(v);
|
||||
case T_ASINH: return asinhf(v);
|
||||
case T_ACOSH: if (v < 1) error(E_FUNC); return acoshf(v);
|
||||
case T_ATANH: if (v <= -1 || v >= 1) error(E_FUNC); return atanhf(v);
|
||||
case T_LOG10: if (v <= 0) error(E_FUNC); return log10f(v);
|
||||
case T_LOG2: if (v <= 0) error(E_FUNC); return log2f(v);
|
||||
case T_CEIL: return -floorf(-v) + 0.0f;
|
||||
case T_IP: return (v < 0 ? -floorf(-v) : floorf(v)) + 0.0f;
|
||||
case T_FP: return v - (v < 0 ? -floorf(-v) : floorf(v));
|
||||
case T_FACT: /* to 34!: 35! is beyond single precision */
|
||||
if (v < 0 || v > 34 || v != floorf(v)) error(E_FUNC);
|
||||
for (w = 1, i = 2; i <= v; i++) w *= i; /* exact to 13! */
|
||||
return w;
|
||||
}
|
||||
error(E_SYNTAX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float func(u8 t)
|
||||
{
|
||||
char s[STRMAX];
|
||||
@@ -796,12 +915,19 @@ static float func(u8 t)
|
||||
return *p ? parse_num(&p) : 0;
|
||||
}
|
||||
v = expr();
|
||||
if (t >= T_ANGLE) return func2(t, v);
|
||||
expect(')');
|
||||
switch (t) {
|
||||
case T_SIN: return sinf(v);
|
||||
case T_COS: return cosf(v);
|
||||
case T_TAN: return tanf(v);
|
||||
case T_ATN: return atanf(v);
|
||||
case T_SIN: return sin_cos(v, 0);
|
||||
case T_COS: return sin_cos(v, 1);
|
||||
case T_TAN:
|
||||
if (deg) {
|
||||
float c = sin_cos(v, 1);
|
||||
if (c == 0) error(E_FUNC);
|
||||
return sin_cos(v, 0) / c;
|
||||
}
|
||||
return tanf(v);
|
||||
case T_ATN: return ang(atanf(v));
|
||||
case T_EXP: return expf(v);
|
||||
case T_LOG: if (v <= 0) error(E_FUNC); return logf(v);
|
||||
case T_ABS: return v < 0 ? -v : v;
|
||||
@@ -852,6 +978,8 @@ static float primary(void)
|
||||
if (is_str_start()) error(E_TYPE);
|
||||
if (c == T_FN) { tp++; return call_fn(); }
|
||||
if (c >= T_SIN && c <= T_POINT && c != T_FN && c != T_TAB) { tp++; return func(c); }
|
||||
if (c == T_PI) { tp++; return PI_F; }
|
||||
if (c >= T_ANGLE && c <= T_FACT) { tp++; return func(c); }
|
||||
if (is_alpha(c)) return *num_ref();
|
||||
error(E_SYNTAX);
|
||||
return 0;
|
||||
@@ -1427,6 +1555,181 @@ static void st_read(void)
|
||||
} while (accept(','));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- block structures */
|
||||
/* Move tp to the start of the next statement, on to the next line if need
|
||||
be. Returns 0 at the end of the program (or of a direct statement). */
|
||||
static u8 next_stmt(void)
|
||||
{
|
||||
skip_statement();
|
||||
if (*tp) { tp++; return 1; }
|
||||
if (!curln || !*(curln + *curln)) return 0;
|
||||
curln += *curln;
|
||||
tp = curln + 3;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Does the statement at p end its line with THEN (a block IF)? */
|
||||
static u8 ends_then(u8 *p)
|
||||
{
|
||||
u8 q = 0, last = 0;
|
||||
for (; *p; p++) {
|
||||
if (*p == '"') q = !q;
|
||||
else if (*p != ' ') last = q ? 0 : *p;
|
||||
}
|
||||
return last == T_THEN;
|
||||
}
|
||||
|
||||
/* Skip to just past the NEXT that closes the FOR we are in, and its
|
||||
variables. */
|
||||
static void find_next(void)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_NONEXT);
|
||||
c = peekc();
|
||||
if (c == T_FOR) depth++;
|
||||
else if (c == T_NEXT && !depth--) {
|
||||
tp++;
|
||||
while (!at_end()) { var_name(); if (!accept(',')) break; }
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip to just past the LOOP that closes the DO we are in, and its
|
||||
condition. */
|
||||
static void find_loop(void)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_NOLOOP);
|
||||
c = peekc();
|
||||
if (c == T_DO) depth++;
|
||||
else if (c == T_LOOP && !depth--) { tp++; skip_statement(); return; }
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip to just past the END IF or END SELECT (k is T_IF or T_SELECT) that
|
||||
closes the block we are in. With els, a block IF's condition was false:
|
||||
stop instead at its ELSE, or at an ELSEIF whose condition is true. */
|
||||
static void find_end(u8 k, u8 els)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(k == T_IF ? E_ENDIF : E_ENDSEL);
|
||||
c = peekc();
|
||||
if (c == k && (k == T_SELECT || ends_then(tp))) depth++;
|
||||
else if (c == T_END) {
|
||||
tp++;
|
||||
if (accept(k) && !depth--) break;
|
||||
} else if (c == T_ELSE && els && !depth) {
|
||||
tp++;
|
||||
if (!accept(T_IF)) break; /* ELSE */
|
||||
c = expr() != 0; /* ELSEIF */
|
||||
expect(T_THEN);
|
||||
if (c) break;
|
||||
}
|
||||
}
|
||||
jumped = 1; /* run on from here */
|
||||
}
|
||||
|
||||
static void st_do(void)
|
||||
{
|
||||
u8 *at = tp - 1, i, go = 1;
|
||||
for (i = 0; i < ndo; i++) /* back at a DO left by GOTO: restart it */
|
||||
if (dos[i].tp == at) { ndo = i; break; }
|
||||
if (accept(T_WHILE)) go = expr() != 0;
|
||||
else if (accept(T_UNTIL)) go = expr() == 0;
|
||||
if (!go) { find_loop(); return; }
|
||||
if (ndo >= DOMAX) error(E_DODEEP);
|
||||
dos[ndo].tp = at;
|
||||
dos[ndo].ln = curln;
|
||||
ndo++;
|
||||
}
|
||||
|
||||
static void st_loop(void)
|
||||
{
|
||||
u8 again = 1;
|
||||
if (!ndo) error(E_NODO);
|
||||
if (accept(T_WHILE)) again = expr() != 0;
|
||||
else if (accept(T_UNTIL)) again = expr() == 0;
|
||||
ndo--;
|
||||
if (again) { /* back to the DO, which tests its condition */
|
||||
tp = dos[ndo].tp;
|
||||
curln = dos[ndo].ln;
|
||||
jumped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void st_exit(void)
|
||||
{
|
||||
if (accept(T_DO)) {
|
||||
if (!ndo) error(E_NODO);
|
||||
ndo--;
|
||||
find_loop();
|
||||
} else if (accept(T_FOR)) {
|
||||
if (!nfor) error(E_NEXT);
|
||||
nfor--;
|
||||
find_next();
|
||||
} else error(E_SYNTAX);
|
||||
}
|
||||
|
||||
/* x compared with the next value: -1, 0 or 1 */
|
||||
static i16 case_cmp(u8 str, float x, const char *xs)
|
||||
{
|
||||
if (str) {
|
||||
char a[STRMAX];
|
||||
i16 c;
|
||||
if (!is_str_start()) error(E_TYPE);
|
||||
sexpr(a);
|
||||
c = strcmp(xs, a);
|
||||
return c < 0 ? -1 : c > 0;
|
||||
} else {
|
||||
float a = expr();
|
||||
return x < a ? -1 : x > a;
|
||||
}
|
||||
}
|
||||
|
||||
/* One item of a CASE list: value, low TO high, or IS relation value. */
|
||||
static u8 case_item(u8 str, float x, const char *xs)
|
||||
{
|
||||
u8 op;
|
||||
i16 c;
|
||||
if (peekc() == 'I' && tp[1] == 'S') tp += 2;
|
||||
op = relop();
|
||||
c = case_cmp(str, x, xs);
|
||||
if (op) return (c < 0 && (op & 1)) || (c == 0 && (op & 2)) || (c > 0 && (op & 4));
|
||||
if (!accept(T_TO)) return c == 0;
|
||||
return (c >= 0) & (case_cmp(str, x, xs) <= 0);
|
||||
}
|
||||
|
||||
/* SELECT CASE: find the first CASE that matches and run on from it. */
|
||||
static void st_select(void)
|
||||
{
|
||||
char s[STRMAX];
|
||||
float x = 0;
|
||||
u8 str = 0, depth = 0, c, m;
|
||||
expect(T_CASE);
|
||||
if (is_str_start()) { sexpr(s); str = 1; }
|
||||
else x = expr();
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_ENDSEL);
|
||||
c = peekc();
|
||||
if (c == T_SELECT) depth++;
|
||||
else if (c == T_END) {
|
||||
tp++;
|
||||
if (accept(T_SELECT) && !depth--) break;
|
||||
} else if (c == T_CASE && !depth) {
|
||||
tp++;
|
||||
if (accept(T_ELSE)) break;
|
||||
m = 0;
|
||||
do m |= case_item(str, x, s); while (accept(','));
|
||||
if (m) break;
|
||||
}
|
||||
}
|
||||
jumped = 1;
|
||||
}
|
||||
|
||||
static void st_for(void)
|
||||
{
|
||||
struct forent *f;
|
||||
@@ -1447,27 +1750,8 @@ static void st_for(void)
|
||||
nfor++;
|
||||
/* skip the body when the loop runs zero times */
|
||||
if ((f->step >= 0 && vars[v] > f->limit) || (f->step < 0 && vars[v] < f->limit)) {
|
||||
u8 depth = 0;
|
||||
nfor--;
|
||||
for (;;) {
|
||||
u8 c;
|
||||
skip_statement();
|
||||
if (!*tp) {
|
||||
if (!curln || !*(curln + *curln)) error(E_FOR);
|
||||
curln += *curln;
|
||||
tp = curln + 3;
|
||||
} else tp++;
|
||||
c = peekc();
|
||||
if (c == T_FOR) depth++;
|
||||
else if (c == T_NEXT) {
|
||||
if (!depth) {
|
||||
tp++;
|
||||
while (!at_end()) { var_name(); if (!accept(',')) break; }
|
||||
return;
|
||||
}
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
find_next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2191,7 +2475,7 @@ static void st_renum(void)
|
||||
rn_put(0); rn_put(n); rn_put(n >> 8);
|
||||
for (q = p + 3; (t = *q++) != 0; ) {
|
||||
rn_put(t);
|
||||
if ((t < T_THEN || t > T_GOSUB) && t != T_RUN) continue; /* THEN GOTO GOSUB */
|
||||
if ((t < T_THEN || t > T_GOSUB) && t != T_RUN && t != T_ELSE) continue; /* THEN GOTO GOSUB ELSE */
|
||||
for (want = 1;;) {
|
||||
while (*q == ' ') rn_put(*q++);
|
||||
if (want && is_digit(*q)) {
|
||||
@@ -2229,8 +2513,22 @@ static void statement(void);
|
||||
static void st_if(void)
|
||||
{
|
||||
float c = expr();
|
||||
u8 depth = 0, q = 0;
|
||||
if (!accept(T_THEN) && peekc() != T_GOTO) error(E_SYNTAX);
|
||||
if (!c) { skip_line(); return; }
|
||||
if (!peekc()) { /* block IF: THEN ends the line */
|
||||
if (!c && curln) find_end(T_IF, 1);
|
||||
return;
|
||||
}
|
||||
if (!c) { /* on to this IF's ELSE, if it has one */
|
||||
for (;; tp++) {
|
||||
if (!*tp) return;
|
||||
if (*tp == '"') q = !q;
|
||||
else if (q) continue;
|
||||
else if (*tp == T_IF) depth++;
|
||||
else if (*tp == T_ELSE && !depth--) break;
|
||||
}
|
||||
tp++;
|
||||
}
|
||||
if (is_digit(peekc())) goto_line(get_lineno());
|
||||
else statement();
|
||||
}
|
||||
@@ -2259,7 +2557,24 @@ static void statement(void)
|
||||
case T_NEXT: st_next(); break;
|
||||
case T_DIM: st_dim(); break;
|
||||
case T_DEF: st_def(); break;
|
||||
case T_END: close_all(); running = 0; break;
|
||||
case T_END:
|
||||
if (accept(T_IF) || accept(T_SELECT)) break;
|
||||
close_all();
|
||||
running = 0;
|
||||
break;
|
||||
case T_DO: st_do(); break;
|
||||
case T_OPTION: /* OPTION ANGLE DEGREES | RADIANS */
|
||||
expect(T_ANGLE);
|
||||
if (accept(T_DEG)) deg = 1;
|
||||
else if (accept(T_RAD)) deg = 0;
|
||||
else error(E_SYNTAX);
|
||||
while (is_alpha(*tp)) tp++; /* the rest of the word */
|
||||
break;
|
||||
case T_LOOP: st_loop(); break;
|
||||
case T_EXIT: st_exit(); break;
|
||||
case T_SELECT: st_select(); break;
|
||||
case T_ELSE: find_end(T_IF, 0); break; /* end of the THEN part */
|
||||
case T_CASE: find_end(T_SELECT, 0); break; /* end of a CASE's part */
|
||||
case T_STOP: error(E_STOP); break;
|
||||
case T_REM: skip_line(); break;
|
||||
case T_DATA: skip_line(); break;
|
||||
@@ -2354,6 +2669,7 @@ static void execute(void)
|
||||
if (!running) break;
|
||||
if (jumped) continue;
|
||||
if (accept(':')) continue;
|
||||
if (peekc() == T_ELSE) skip_line(); /* IF ... THEN's statement ran */
|
||||
if (peekc()) error(E_SYNTAX);
|
||||
if (!curln) break; /* end of a direct statement */
|
||||
curln += *curln;
|
||||
@@ -2377,7 +2693,7 @@ int main(void)
|
||||
newline();
|
||||
if (setjmp(errjmp)) {
|
||||
running = 0;
|
||||
nfor = nsub = 0;
|
||||
nfor = nsub = ndo = 0;
|
||||
curln = 0;
|
||||
}
|
||||
for (;;) {
|
||||
|
||||
+3034
-3034
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Try BASIC's block structures (DO ... LOOP, EXIT, IF ... ELSE ... END IF,
|
||||
SELECT CASE) in tools/rv32emu.py, with the screen kept by pyte.
|
||||
|
||||
python3 test-blocks.py
|
||||
"""
|
||||
import importlib.util
|
||||
import os
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
|
||||
te = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(te)
|
||||
CR = te.CR
|
||||
|
||||
|
||||
def main():
|
||||
b = te.Basic()
|
||||
errors = 0
|
||||
|
||||
def check(what, ok, out=""):
|
||||
nonlocal errors
|
||||
print(("PASS " if ok else "FAIL ") + what)
|
||||
if not ok:
|
||||
print(out)
|
||||
errors += not ok
|
||||
|
||||
def screen():
|
||||
return "\n".join(l.rstrip() for l in b.screen_text().display)
|
||||
|
||||
def run(prog):
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
for line in prog.strip().splitlines():
|
||||
b.type(line.encode() + CR)
|
||||
b.type(b"RUN" + CR)
|
||||
return screen().split("RUN")[-1]
|
||||
|
||||
b.type(b"")
|
||||
out = run('''
|
||||
10 I = 0
|
||||
20 DO WHILE I < 5
|
||||
30 PRINT I;
|
||||
40 I = I + 1
|
||||
50 LOOP
|
||||
60 PRINT "/";
|
||||
70 DO
|
||||
80 I = I - 1: PRINT I;
|
||||
90 LOOP UNTIL I = 0
|
||||
100 PRINT "/";
|
||||
110 DO UNTIL 1: PRINT "NEVER": LOOP
|
||||
120 DO WHILE 0
|
||||
130 PRINT "NEVER"
|
||||
140 LOOP WHILE 1
|
||||
150 PRINT "END"
|
||||
''')
|
||||
check("DO WHILE, LOOP UNTIL, and loops that run no times",
|
||||
"0 1 2 3 4 / 4 3 2 1 0 /END" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 3
|
||||
20 J = 0
|
||||
30 DO
|
||||
40 J = J + 1
|
||||
50 IF J > I THEN EXIT DO
|
||||
60 PRINT J;
|
||||
70 LOOP
|
||||
80 PRINT ",";
|
||||
90 NEXT I
|
||||
100 FOR K = 1 TO 100
|
||||
110 IF K = 4 THEN EXIT FOR
|
||||
120 NEXT K
|
||||
130 PRINT K
|
||||
''')
|
||||
check("nested DO in FOR, EXIT DO, EXIT FOR", "1 , 1 2 , 1 2 3 , 4" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 4
|
||||
20 IF I = 1 THEN
|
||||
30 PRINT "ONE";
|
||||
40 ELSEIF I = 2 THEN
|
||||
50 PRINT "TWO";
|
||||
60 IF I > 0 THEN
|
||||
70 PRINT "+";
|
||||
80 ELSE
|
||||
90 PRINT "NEVER";
|
||||
100 END IF
|
||||
110 ELSE IF I = 3 THEN
|
||||
120 PRINT "THREE";
|
||||
130 ELSE
|
||||
140 PRINT "MANY";
|
||||
150 END IF
|
||||
160 PRINT ",";
|
||||
170 NEXT I
|
||||
''')
|
||||
check("block IF, ELSEIF, ELSE IF, ELSE, nested, END IF",
|
||||
"ONE,TWO+,THREE,MANY," in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 3
|
||||
20 IF I = 2 THEN PRINT "B"; ELSE PRINT "A";
|
||||
30 IF I = 1 THEN 50 ELSE PRINT "-";
|
||||
40 GOTO 60
|
||||
50 PRINT "*";
|
||||
60 NEXT I
|
||||
70 IF 1 THEN IF 0 THEN PRINT "X" ELSE PRINT "Y" ELSE PRINT "Z"
|
||||
80 IF 0 THEN IF 1 THEN PRINT "X" ELSE PRINT "Y" ELSE PRINT "Z"
|
||||
90 IF 0 THEN PRINT "ELSE" ELSE PRINT "ELSE OK"
|
||||
''')
|
||||
check("one-line IF ... ELSE, with line numbers and nesting",
|
||||
"A*B-A-Y\nZ\nELSE OK" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 0 TO 7
|
||||
20 SELECT CASE I * 2
|
||||
30 CASE 0
|
||||
40 PRINT "ZERO";
|
||||
50 CASE 2, 4
|
||||
60 PRINT "SMALL";
|
||||
70 CASE 6 TO 9
|
||||
80 PRINT "MID";
|
||||
90 CASE IS >= 12
|
||||
100 PRINT "BIG";
|
||||
110 CASE ELSE
|
||||
120 PRINT "OTHER";
|
||||
130 END SELECT
|
||||
140 PRINT ",";
|
||||
150 NEXT I
|
||||
160 A$ = "PEAR"
|
||||
170 SELECT CASE A$
|
||||
180 CASE "APPLE": PRINT "A"
|
||||
190 CASE "B" TO "Q": PRINT "IN RANGE"
|
||||
200 END SELECT
|
||||
210 SELECT CASE 5: CASE 1: PRINT "NO": END SELECT
|
||||
220 PRINT "DONE"
|
||||
''')
|
||||
check("SELECT CASE numbers: values, TO, IS, ELSE",
|
||||
"ZERO,SMALL,SMALL,MID,MID,OTHER,BIG,BIG," in out, out)
|
||||
check("SELECT CASE strings, no CASE matching", "IN RANGE\nDONE" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 X = 0
|
||||
20 DO
|
||||
30 X = X + 1: PRINT X;
|
||||
40 IF X < 30 THEN GOTO 20
|
||||
50 IF X > 40 THEN EXIT DO
|
||||
60 LOOP
|
||||
70 PRINT "END"
|
||||
''')
|
||||
check("GOTO back to a DO does not fill its stack", " 40 41 END" in out, out)
|
||||
|
||||
b.type(b"CLS" + CR)
|
||||
b.type(b'I=0: DO: I=I+1: LOOP UNTIL I=7: PRINT "DIRECT"; I' + CR)
|
||||
check("DO ... LOOP in a direct statement", "DIRECT 7" in screen(), screen())
|
||||
|
||||
out = run('10 LOOP')
|
||||
check("LOOP without DO", "LOOP WITHOUT DO" in out, out)
|
||||
out = run('10 DO WHILE 0\n20 PRINT')
|
||||
check("DO without LOOP", "DO WITHOUT LOOP" in out, out)
|
||||
out = run('10 IF 0 THEN\n20 PRINT')
|
||||
check("IF without END IF", "IF WITHOUT END IF" in out, out)
|
||||
out = run('10 SELECT CASE 1\n20 PRINT')
|
||||
check("SELECT without END SELECT", "SELECT WITHOUT END SELECT" in out, out)
|
||||
out = run('10 FOR I = 2 TO 1\n20 PRINT')
|
||||
check("FOR without NEXT", "FOR WITHOUT NEXT" in out, out)
|
||||
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
for l in (b"10 IF A THEN 30 ELSE 40", b"30 END IF", b"40 END"):
|
||||
b.type(l + CR)
|
||||
b.type(b"RENUM 100" + CR)
|
||||
b.type(b"LIST" + CR)
|
||||
check("RENUM follows ELSE's line number", "100 IF A THEN 110 ELSE 120" in screen(), screen())
|
||||
sys.exit(1 if errors else 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,281 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check BASIC's scientific functions in tools/rv32emu.py.
|
||||
|
||||
python3 test-math.py [count]
|
||||
|
||||
First the maths library itself: each function in basic.bin (lib/mathf.c) is
|
||||
called directly in the emulator, on random values across its range and on
|
||||
the awkward ones, and its result compared bit for bit with Python's math
|
||||
module (in double precision, so as good as exact here), in units in the last
|
||||
place of single precision. Then BASIC: the functions as statements see them,
|
||||
OPTION ANGLE DEGREES, and the errors outside each function's domain.
|
||||
"""
|
||||
import importlib.util
|
||||
import math
|
||||
import os
|
||||
import random
|
||||
import struct
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
|
||||
te = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(te)
|
||||
CR = te.CR
|
||||
|
||||
RET = 0x12000 # the machine-code area: a return address never run
|
||||
STACK = 0x12FF0
|
||||
|
||||
|
||||
def f32(x):
|
||||
return struct.unpack("<f", struct.pack("<f", x))[0]
|
||||
|
||||
|
||||
def bits(x):
|
||||
return struct.unpack("<I", struct.pack("<f", x))[0]
|
||||
|
||||
|
||||
def unbits(i):
|
||||
return struct.unpack("<f", struct.pack("<I", i & 0xFFFFFFFF))[0]
|
||||
|
||||
|
||||
def ulp(x):
|
||||
"""Single precision's unit in the last place at x."""
|
||||
x = abs(x)
|
||||
if x < 2.0 ** -126:
|
||||
return 2.0 ** -149
|
||||
return 2.0 ** (math.floor(math.log2(x)) - 23)
|
||||
|
||||
|
||||
def ulps(got, want):
|
||||
if math.isinf(want):
|
||||
return 0 if got == want else math.inf
|
||||
w = f32(want) if abs(want) < 3.4e38 else want
|
||||
if math.isinf(w):
|
||||
return 0 if math.isinf(got) and (got > 0) == (want > 0) else math.inf
|
||||
return abs(got - want) / ulp(want)
|
||||
|
||||
|
||||
def symbols():
|
||||
s = {}
|
||||
for line in open(os.path.join(HERE, "out", "basic.sym")):
|
||||
a, t, n = line.split()
|
||||
s[n] = int(a, 16)
|
||||
return s
|
||||
|
||||
|
||||
class Caller:
|
||||
"""Call a float function in basic.bin, ilp32f: arguments in fa0, fa1."""
|
||||
|
||||
def __init__(self, board):
|
||||
self.b = board
|
||||
self.sym = symbols()
|
||||
|
||||
def __call__(self, name, *args):
|
||||
b = self.b
|
||||
x, f, pc = b.x[:], b.f[:], b.pc
|
||||
for i, a in enumerate(args):
|
||||
b.f[10 + i] = bits(a)
|
||||
b.x[1] = RET
|
||||
b.x[2] = STACK
|
||||
b.pc = self.sym[name]
|
||||
n = 0
|
||||
while b.pc != RET:
|
||||
b.step()
|
||||
n += 1
|
||||
if n > 100000:
|
||||
raise RuntimeError(name + " did not return")
|
||||
r = unbits(b.f[10])
|
||||
b.x[:], b.f[:], b.pc = x, f, pc
|
||||
return r
|
||||
|
||||
|
||||
def logspace(rng, lo, hi, n, signed=False):
|
||||
"""n random floats with magnitudes spread evenly in log between lo and hi"""
|
||||
out = []
|
||||
for _ in range(n):
|
||||
v = f32(math.exp(rng.uniform(math.log(lo), math.log(hi))))
|
||||
out.append(-v if signed and rng.random() < 0.5 else v)
|
||||
return out
|
||||
|
||||
|
||||
def main():
|
||||
count = int(sys.argv[1]) if len(sys.argv) > 1 else 1500
|
||||
rng = random.Random(1)
|
||||
b = te.Basic()
|
||||
b.type(b"")
|
||||
call = Caller(b)
|
||||
errors = 0
|
||||
|
||||
def check(what, ok, out=""):
|
||||
nonlocal errors
|
||||
print(("PASS " if ok else "FAIL ") + what)
|
||||
if not ok and out:
|
||||
print(out)
|
||||
errors += not ok
|
||||
|
||||
def uni(lo, hi, n):
|
||||
return [f32(rng.uniform(lo, hi)) for _ in range(n)]
|
||||
|
||||
# name, reference, inputs, the most ulps allowed
|
||||
n = count
|
||||
tests = [
|
||||
("asinf", math.asin, uni(-1, 1, n) + logspace(rng, 1e-30, 1, n, True) + [1, -1, 0.5, 1 - 2 ** -24], 3),
|
||||
("acosf", math.acos, uni(-1, 1, n) + logspace(rng, 1e-30, 1, n, True) + [1, -1, 0, 1 - 2 ** -24, -1 + 2 ** -24], 3),
|
||||
("sinhf", math.sinh, uni(-12, 12, n) + logspace(rng, 1e-30, 89, n, True) + [1, -1, 10, 89.4], 3),
|
||||
("coshf", math.cosh, uni(-12, 12, n) + logspace(rng, 1e-30, 89, n, True) + [0, 1, 10, 89.4], 3),
|
||||
("tanhf", math.tanh, uni(-12, 12, n) + logspace(rng, 1e-30, 30, n, True) + [0, 1, 10, -10], 3),
|
||||
("asinhf", math.asinh, uni(-10, 10, n) + logspace(rng, 1e-30, 3e38, n, True) + [0, 1, 1e9, 2e9], 3),
|
||||
("acoshf", math.acosh, [1 + abs(v) for v in uni(0, 10, n)] + logspace(rng, 1, 3e38, n)
|
||||
+ [1 + 2 ** -23 * k for k in range(1, 40)] + [1, 1e9, 2e9], 3),
|
||||
("atanhf", math.atanh, uni(-1, 1, n) + logspace(rng, 1e-30, 1, n, True)
|
||||
+ [1 - 2 ** -24 * k for k in range(1, 40)] + [0, 0.5], 3),
|
||||
("log1pf", math.log1p, logspace(rng, 1e-30, 1e30, n) + uni(-0.999, 1, n), 3),
|
||||
("log10f", math.log10, logspace(rng, 1e-45, 3e38, n) + uni(0.5, 2, n) + [10.0 ** k for k in range(-37, 39)], 2),
|
||||
("log2f", math.log2, logspace(rng, 1e-45, 3e38, n) + uni(0.5, 2, n), 2),
|
||||
]
|
||||
for name, ref, xs, most in tests:
|
||||
worst, at = 0, None
|
||||
for x in xs:
|
||||
x = f32(x)
|
||||
try:
|
||||
want = ref(x)
|
||||
except (ValueError, OverflowError):
|
||||
want = math.inf if name in ("sinhf", "coshf") else None
|
||||
if name == "sinhf":
|
||||
want = math.copysign(math.inf, x)
|
||||
if want is None:
|
||||
continue
|
||||
e = ulps(call(name, x), want)
|
||||
if e > worst:
|
||||
worst, at = e, x
|
||||
check(f"{name}: {len(xs)} values, worst {worst:.2f} ulp (at {at!r}), "
|
||||
f"allowed {most}", worst <= most)
|
||||
# exact cases
|
||||
check("log2f exact at powers of 2", all(call("log2f", 2.0 ** k) == k for k in range(-149, 128)))
|
||||
check("asinf(1), acosf(-1), acosf(1) exact",
|
||||
call("asinf", 1.0) == f32(math.pi / 2) and call("acosf", -1.0) == f32(math.pi)
|
||||
and call("acosf", 1.0) == 0)
|
||||
|
||||
# atan2f
|
||||
worst, at = 0, None
|
||||
for _ in range(4 * n):
|
||||
y, x = f32(rng.uniform(-10, 10)), f32(rng.uniform(-10, 10))
|
||||
if rng.random() < 0.3:
|
||||
y = f32(y * 10 ** rng.uniform(-20, 20))
|
||||
e = ulps(call("atan2f", y, x), math.atan2(y, x))
|
||||
if e > worst:
|
||||
worst, at = e, (y, x)
|
||||
axes = all(call("atan2f", y, x) == f32(math.atan2(y, x))
|
||||
for y, x in [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (-1, -1)])
|
||||
check(f"atan2f: {4 * n} values, worst {worst:.2f} ulp (at {at!r}), allowed 3; axes exact",
|
||||
worst <= 3 and axes)
|
||||
|
||||
# fmodf must be exact
|
||||
bad = []
|
||||
for _ in range(4 * n):
|
||||
x = f32(rng.choice([-1, 1]) * 10 ** rng.uniform(-10, 30))
|
||||
y = f32(rng.choice([-1, 1]) * 10 ** rng.uniform(-10, 10))
|
||||
if call("fmodf", x, y) != f32(math.fmod(x, y)):
|
||||
bad.append((x, y))
|
||||
check(f"fmodf exact on {4 * n} values", not bad, repr(bad[:5]))
|
||||
|
||||
# ---------------------------------------------------------------- BASIC
|
||||
def screen():
|
||||
return "\n".join(l.rstrip() for l in b.screen_text().display)
|
||||
|
||||
def run(prog):
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
for line in prog.strip().splitlines():
|
||||
b.type(line.encode() + CR)
|
||||
b.type(b"RUN" + CR)
|
||||
return screen().split("RUN")[-1]
|
||||
|
||||
def rows(out):
|
||||
return [l.strip() for l in out.splitlines() if l.strip() and l.strip() != "READY"]
|
||||
|
||||
out = run('''
|
||||
10 PRINT PI; DEG(PI); RAD(180); ATN(1) * 4
|
||||
20 PRINT ASIN(.5); ACOS(-1); ANGLE(-1, 0); ANGLE(0, -2)
|
||||
30 PRINT SINH(1); COSH(1); TANH(.5); ASINH(1); ACOSH(2); ATANH(.5)
|
||||
40 PRINT LOG10(1000); LOG2(1024); LOG10(2); LOG(EXP(2))
|
||||
50 PRINT SEC(0); CSC(PI / 2); COT(PI / 4)
|
||||
''')
|
||||
got = rows(out)
|
||||
want = ["3.14159 180 3.14159 3.14159",
|
||||
".523599 3.14159 3.14159 -1.5708",
|
||||
"1.1752 1.54308 .462117 .881374 1.31696 .549306",
|
||||
"3 10 .30103 2",
|
||||
"1 1 1"]
|
||||
check("PI, DEG, RAD, the inverse trig functions, ANGLE", got[:2] == want[:2], out)
|
||||
check("hyperbolic functions and their inverses", got[2:3] == want[2:3], out)
|
||||
check("LOG10, LOG2", got[3:4] == want[3:4], out)
|
||||
check("SEC, CSC, COT", got[4:5] == want[4:5], out)
|
||||
|
||||
out = run('''
|
||||
10 PRINT ROUND(2.567, 2); ROUND(2.5); ROUND(-2.5); ROUND(1250, -2); ROUND(-1.25, 1)
|
||||
20 PRINT TRUNCATE(2.567, 1); TRUNCATE(-2.567, 1); TRUNCATE(1299, -2)
|
||||
30 PRINT IP(3.7); IP(-3.7); FP(3.75); FP(-3.75); CEIL(3.2); CEIL(-3.2); CEIL(4)
|
||||
40 PRINT MOD(7, 3); MOD(-7, 3); MOD(7, -3); MOD(-7, -3); MOD(7.5, 2); MOD(6, 3)
|
||||
50 PRINT MIN(3, 1, 2); MAX(3, 1, 2); MIN(-1, -5); MAX(4)
|
||||
60 PRINT FACT(0); FACT(5); FACT(10); FACT(13); FACT(34)
|
||||
''')
|
||||
got = rows(out)
|
||||
want = ["2.57 3 -2 1300 -1.2",
|
||||
"2.5 -2.5 1200",
|
||||
"3 -3 .75 -.75 4 -3 4",
|
||||
"1 2 -2 -1 1.5 0",
|
||||
"1 3 -5 4",
|
||||
"1 120 3.6288E+06 6.22702E+09 2.95233E+38"]
|
||||
for w, g, what in zip(want, got + [""] * 6,
|
||||
["ROUND", "TRUNCATE", "IP, FP, CEIL", "MOD", "MIN, MAX", "FACT"]):
|
||||
check(what, g == w, f"got {g!r}, want {w!r}")
|
||||
|
||||
out = run('''
|
||||
10 OPTION ANGLE DEGREES
|
||||
20 PRINT SIN(30); SIN(180); COS(90); COS(60); TAN(45); SIN(-90); COS(720)
|
||||
30 PRINT ASIN(1); ACOS(0); ATN(1); ANGLE(-1, -1); SEC(60); COT(45); CSC(30)
|
||||
40 PRINT SIN(1E6); COS(-123.5)
|
||||
50 OPTION ANGLE RADIANS
|
||||
60 PRINT SIN(PI / 6); ATN(1)
|
||||
''')
|
||||
got = rows(out)
|
||||
check("OPTION ANGLE DEGREES: exact at multiples of 30 and 45",
|
||||
got[:2] == [".5 0 0 .5 1 -1 1", "90 90 45 -135 2 1 2"], out)
|
||||
try:
|
||||
big = [float(v) for v in got[2].split()]
|
||||
except (IndexError, ValueError):
|
||||
big = []
|
||||
check("degrees: large and negative angles",
|
||||
len(big) == 2 and abs(big[0] - math.sin(math.radians(1e6))) < 2e-6
|
||||
and abs(big[1] - math.cos(math.radians(-123.5))) < 2e-6, out)
|
||||
check("OPTION ANGLE RADIANS goes back", got[3:4] == [".5 .785398"], out)
|
||||
out = run('10 PRINT SIN(90)')
|
||||
check("RUN starts in radians again", rows(out) == [".893997"], out)
|
||||
|
||||
for expr, msg in [("ASIN(1.5)", "ILLEGAL"), ("ACOS(-2)", "ILLEGAL"), ("ACOSH(.5)", "ILLEGAL"),
|
||||
("ATANH(1)", "ILLEGAL"), ("ATANH(-1)", "ILLEGAL"), ("LOG10(0)", "ILLEGAL"),
|
||||
("LOG2(-1)", "ILLEGAL"), ("FACT(-1)", "ILLEGAL"), ("FACT(2.5)", "ILLEGAL"),
|
||||
("FACT(35)", "ILLEGAL"), ("ANGLE(0, 0)", "ILLEGAL"), ("MOD(1, 0)", "DIVISION BY ZERO"),
|
||||
("ROUND(1, 39)", "ILLEGAL"), ("CSC(0)", "ILLEGAL"), ("COT(0)", "ILLEGAL")]:
|
||||
out = run(f"10 PRINT {expr}")
|
||||
check(f"{expr} is an error", msg in out, out)
|
||||
out = run("10 OPTION ANGLE DEGREES: PRINT TAN(90)")
|
||||
check("TAN(90) in degrees is an error", "ILLEGAL" in out, out)
|
||||
out = run("10 OPTION ANGLE DEGREES: PRINT SEC(90)")
|
||||
check("SEC(90) in degrees is an error", "ILLEGAL" in out, out)
|
||||
out = run("10 OPTION ANGLE GRADS")
|
||||
check("OPTION ANGLE wants DEGREES or RADIANS", "SYNTAX" in out, out)
|
||||
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
b.type(b"10 OPTION ANGLE DEGREES: X = TRUNCATE(PI, 3)" + CR)
|
||||
b.type(b"LIST" + CR)
|
||||
check("LIST spells the new words as typed",
|
||||
"10 OPTION ANGLE DEGREES: X = TRUNCATE(PI, 3)" in screen(), screen())
|
||||
sys.exit(1 if errors else 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+169
-13
@@ -99,7 +99,7 @@ L End Enter now it is 10 PRINT "HELLO"
|
||||
|
||||
`RENUM` gives the lines new numbers, starting at 10 (or the first number
|
||||
given) and going up in tens (or the second). It also changes the line numbers
|
||||
after `GOTO`, `GOSUB`, `THEN`, `RUN` and `ON … GOTO`/`GOSUB` so the program
|
||||
after `GOTO`, `GOSUB`, `THEN`, `ELSE`, `RUN` and `ON … GOTO`/`GOSUB` so the program
|
||||
runs as before. A number that isn't a line in the program is left alone.
|
||||
Numbers inside quotes, `REM` and `DATA` are left as they are, and so is a
|
||||
line number worked out at run time. If the new numbers would pass 65535, or
|
||||
@@ -270,6 +270,37 @@ by statements to run. When it is false, the rest of the line is skipped, so
|
||||
statements after the colon run only when the condition is true. Any non-zero
|
||||
value counts as true.
|
||||
|
||||
`ELSE` gives the line a second part, run only when the condition is false:
|
||||
|
||||
```
|
||||
IF X > 100 THEN 500 ELSE 600
|
||||
IF A$ = "YES" THEN PRINT "OK": GOTO 20 ELSE PRINT "NO"
|
||||
IF A THEN IF B THEN PRINT "BOTH" ELSE PRINT "A ONLY" ELSE PRINT "NOT A"
|
||||
```
|
||||
|
||||
An `ELSE` belongs to the nearest `IF` before it that has none yet.
|
||||
|
||||
**Block IF.** When `THEN` is the last thing on its line, the statements that
|
||||
follow, down to `END IF`, are the `THEN` part, and the block can have
|
||||
`ELSEIF` and `ELSE` parts too:
|
||||
|
||||
```
|
||||
10 IF T < 0 THEN
|
||||
20 PRINT "FREEZING"
|
||||
30 C = 1
|
||||
40 ELSEIF T < 20 THEN
|
||||
50 PRINT "COOL"
|
||||
60 ELSE
|
||||
70 PRINT "WARM"
|
||||
80 END IF
|
||||
```
|
||||
|
||||
Only the first part whose condition is true runs (or `ELSE`'s, if none is);
|
||||
then the program carries on after `END IF`. Blocks nest inside each other and
|
||||
inside loops. `ELSEIF` may be written `ELSE IF`, and `END IF` `ENDIF`. Each
|
||||
`ELSEIF` must end its line with `THEN`. Block IFs work in a program, not in
|
||||
a direct statement.
|
||||
|
||||
### GOTO, GOSUB, RETURN, ON
|
||||
|
||||
```
|
||||
@@ -304,6 +335,56 @@ NEXT J, I
|
||||
- Loops nest 16 deep.
|
||||
- Jumping out of a loop with `GOTO` is allowed. Starting a new `FOR` with the
|
||||
same variable discards the old loop.
|
||||
- `EXIT FOR` leaves the innermost loop at once, carrying on after its `NEXT`;
|
||||
the variable keeps the value it had.
|
||||
|
||||
### DO … LOOP — loops on a condition
|
||||
|
||||
```
|
||||
10 DO WHILE I < 10
|
||||
20 I = I + 1
|
||||
30 LOOP
|
||||
|
||||
10 DO
|
||||
20 INPUT "GUESS"; G
|
||||
30 LOOP UNTIL G = 7
|
||||
```
|
||||
|
||||
- `DO WHILE` *condition* runs the loop while the condition is true, testing it
|
||||
before each time round, so the body may not run at all. `DO UNTIL`
|
||||
*condition* runs it until the condition is true.
|
||||
- `LOOP WHILE` and `LOOP UNTIL` test at the end instead, so the body always
|
||||
runs at least once.
|
||||
- A plain `DO` … `LOOP` goes round for ever, until `EXIT DO` (or `GOTO`, `END`
|
||||
or Ctrl-C) leaves it. `EXIT DO` carries on after the `LOOP`.
|
||||
- Loops nest 16 deep, with `FOR` loops inside and out. A whole loop can be on
|
||||
one line, or in a direct statement:
|
||||
`I = 0: DO: I = I + 1: PRINT I: LOOP UNTIL I = 5`.
|
||||
|
||||
### SELECT CASE — choosing by value
|
||||
|
||||
```
|
||||
10 SELECT CASE N
|
||||
20 CASE 0
|
||||
30 PRINT "NONE"
|
||||
40 CASE 1, 2, 3
|
||||
50 PRINT "A FEW"
|
||||
60 CASE 4 TO 9
|
||||
70 PRINT "SEVERAL"
|
||||
80 CASE IS >= 10
|
||||
90 PRINT "LOTS"
|
||||
100 CASE ELSE
|
||||
110 PRINT "LESS THAN NONE"
|
||||
120 END SELECT
|
||||
```
|
||||
|
||||
`SELECT CASE` works out its value once, then runs the statements under the
|
||||
first `CASE` that matches it and carries on after `END SELECT`. A `CASE` lists
|
||||
values, ranges (*low* `TO` *high*) and comparisons (`IS` followed by `<`,
|
||||
`<=`, `=`, `<>`, `>=` or `>` and a value), separated by commas; any one of
|
||||
them matching is enough. `CASE ELSE` matches anything, so it goes last. When
|
||||
no `CASE` matches, nothing runs. The value may be a string, with strings in
|
||||
the `CASE`s: `CASE "A" TO "M"` compares in alphabetical order, as `<` does.
|
||||
|
||||
### DIM — arrays
|
||||
|
||||
@@ -338,6 +419,16 @@ in its formula are the program's ordinary variables.
|
||||
the program. A `DATA` statement takes up the rest of its line. `RESTORE` starts
|
||||
again from the first item.
|
||||
|
||||
### OPTION ANGLE
|
||||
|
||||
```
|
||||
OPTION ANGLE DEGREES
|
||||
OPTION ANGLE RADIANS
|
||||
```
|
||||
|
||||
Chooses the units of the trigonometric functions (section 6). Radians are
|
||||
the default, and `RUN` goes back to them.
|
||||
|
||||
### END, STOP, REM
|
||||
|
||||
- `END` stops the program quietly.
|
||||
@@ -379,16 +470,74 @@ and `INK` are described in sections 7 and 8.
|
||||
| `ABS(X)` | Absolute value |
|
||||
| `SGN(X)` | -1, 0 or 1 |
|
||||
| `INT(X)` | Largest whole number not above *X*: `INT(-2.5)` is -3 |
|
||||
| `IP(X)` | The whole-number part, towards 0: `IP(-2.5)` is -2 |
|
||||
| `FP(X)` | The fraction part, `X - IP(X)`: `FP(-2.5)` is -.5 |
|
||||
| `CEIL(X)` | Smallest whole number not below *X*: `CEIL(2.1)` is 3 |
|
||||
| `ROUND(X [, N])` | *X* rounded to *N* decimal places (0 if left out); a negative *N* rounds to tens, hundreds…: `ROUND(2.567, 2)` is 2.57, `ROUND(1250, -2)` is 1300. Halves go up, as `INT(X + .5)` does: `ROUND(-2.5)` is -2 |
|
||||
| `TRUNCATE(X, N)` | *X* cut short after *N* decimal places: `TRUNCATE(-2.567, 1)` is -2.5 |
|
||||
| `MOD(A, B)` | Remainder of *A* divided by *B*, with the sign of *B*: `A - B * INT(A / B)`, but exact. `MOD(-7, 3)` is 2 |
|
||||
| `MIN(A, B, …)`, `MAX(A, B, …)` | Smallest or largest of any number of values |
|
||||
| `FACT(N)` | *N* factorial, for whole numbers 0 to 34 |
|
||||
| `SQR(X)` | Square root |
|
||||
| `EXP(X)` | e to the power *X* |
|
||||
| `LOG(X)` | Natural logarithm |
|
||||
| `SIN(X)`, `COS(X)`, `TAN(X)` | Trigonometry, in radians |
|
||||
| `ATN(X)` | Arctangent, in radians; `4*ATN(1)` is pi |
|
||||
| `LOG10(X)`, `LOG2(X)` | Logarithm to base 10 or 2 (`LOG2` is exact for powers of 2); powers of 10 are `10 ^ X` |
|
||||
| `PI` | 3.14159265 (no brackets) |
|
||||
| `RND` or `RND(X)` | Random number from 0 up to (not including) 1; *X* is ignored |
|
||||
| `FNx(X)` | A function made with `DEF` |
|
||||
|
||||
A whole number from 1 to 6: `1 + INT(RND * 6)`.
|
||||
|
||||
### Trigonometry
|
||||
|
||||
| Function | Result |
|
||||
|---|---|
|
||||
| `SIN(X)`, `COS(X)`, `TAN(X)` | Sine, cosine, tangent |
|
||||
| `SEC(X)`, `CSC(X)`, `COT(X)` | Secant, cosecant, cotangent: `1/COS(X)`, `1/SIN(X)`, `1/TAN(X)` |
|
||||
| `ATN(X)` | Arctangent, from -90° to 90° (-π/2 to π/2) |
|
||||
| `ASIN(X)`, `ACOS(X)` | Arcsine (-90° to 90°) and arccosine (0° to 180°), for *X* from -1 to 1 |
|
||||
| `ANGLE(X, Y)` | The direction of the point (*X*, *Y*) from the origin, from -180° to 180°, measured anticlockwise from the X axis; it gets the quarter right, as `ATN(Y / X)` can't (other languages call it `ATAN2(Y, X)`) |
|
||||
| `DEG(X)`, `RAD(X)` | Radians to degrees, degrees to radians |
|
||||
| `SINH(X)`, `COSH(X)`, `TANH(X)` | Hyperbolic sine, cosine, tangent |
|
||||
| `ASINH(X)`, `ACOSH(X)`, `ATANH(X)` | Their inverses: `ACOSH` for *X* ≥ 1, `ATANH` for *X* between -1 and 1 |
|
||||
|
||||
Angles are in radians until a program says `OPTION ANGLE DEGREES`; from
|
||||
then on the trigonometric functions take degrees and the inverse ones
|
||||
(`ATN`, `ASIN`, `ACOS`, `ANGLE`) give degrees. `OPTION ANGLE RADIANS` goes
|
||||
back, and so does `RUN`. In degrees, `SIN`, `COS` and the rest are exact at
|
||||
multiples of 90, so `SIN(180)` is 0, not a tiny remainder.
|
||||
|
||||
A value outside a function's range, such as `ASIN(2)`, `LOG10(0)`,
|
||||
`ACOSH(0)`, `FACT(2.5)`, `ANGLE(0, 0)` or, in degrees, `TAN(90)`, stops
|
||||
the program with `ILLEGAL FUNCTION CALL`; `MOD(A, 0)` is
|
||||
`DIVISION BY ZERO`. Every function is correct to within 3 units in the last
|
||||
place of single precision (about 1 part in 3 million), checked against a
|
||||
PC's double-precision maths by `basic/test-math.py`.
|
||||
|
||||
```
|
||||
10 OPTION ANGLE DEGREES
|
||||
20 FOR A = 0 TO 75 STEP 15
|
||||
30 PRINT A, ROUND(SIN(A), 4), ROUND(COS(A), 4), ROUND(TAN(A), 4)
|
||||
40 NEXT A
|
||||
50 PRINT "BEARING OF (3, 4):"; ROUND(ANGLE(3, 4), 2); "DEGREES"
|
||||
60 PRINT "ITS HYPOTENUSE:"; SQR(3^2 + 4^2)
|
||||
70 PRINT "ACOS(-1) ="; ACOS(-1); " 20! ="; FACT(20)
|
||||
```
|
||||
|
||||
prints
|
||||
|
||||
```
|
||||
0 0 1 0
|
||||
15 .2588 .9659 .2679
|
||||
30 .5 .866 .5774
|
||||
45 .7071 .7071 1
|
||||
60 .866 .5 1.7321
|
||||
75 .9659 .2588 3.7321
|
||||
BEARING OF (3, 4): 53.13 DEGREES
|
||||
ITS HYPOTENUSE: 5
|
||||
ACOS(-1) = 180 20! = 2.4329E+18
|
||||
```
|
||||
|
||||
### Strings
|
||||
|
||||
| Function | Result |
|
||||
@@ -796,19 +945,20 @@ each voice, so a note or a bang fades away by itself.
|
||||
### Speed
|
||||
|
||||
The processor is a 32-bit RISC-V (RV32IMF: the base instructions, multiply
|
||||
and divide, and single-precision floating point) running at 90 MHz. Most
|
||||
and divide, and single-precision floating point) running at 100 MHz. Most
|
||||
instructions take 2 clocks; loads, stores, jumps and taken branches 3, a
|
||||
multiply 4 and a divide 35, which comes to about 2.5 clocks per instruction in
|
||||
BASIC, some 36 million instructions a second. Its floating-point unit adds,
|
||||
subtracts and multiplies in 12 clocks and divides or takes a square root in 36,
|
||||
BASIC, some 40 million instructions a second. Its floating-point unit adds,
|
||||
subtracts and multiplies in 13 clocks and divides or takes a square root in 37,
|
||||
correctly rounded. In round numbers:
|
||||
|
||||
| Operation | Time |
|
||||
|---|---|
|
||||
| One pass of an empty `FOR … NEXT` loop | 8 µs (about 120,000 a second) |
|
||||
| `X = I * 1.5` | 26 µs |
|
||||
| `Y = SIN(X)` | 37 µs |
|
||||
| `LINE` right across the screen | 0.2 ms |
|
||||
| One pass of an empty `FOR … NEXT` loop | 6.4 µs (about 150,000 a second) |
|
||||
| `X = I * 1.5` | 23 µs |
|
||||
| `Y = SIN(X)` | 33 µs |
|
||||
| `Y = EXP(1)` | 33 µs |
|
||||
| `LINE` right across the screen | 0.12 ms |
|
||||
|
||||
Constants in a program are converted from text every time they are used, so a
|
||||
loop runs a little faster if a constant used inside it is put into a variable
|
||||
@@ -1221,13 +1371,19 @@ An error stops the program and reports the line, for example
|
||||
| `NEXT WITHOUT FOR` | `NEXT` with no loop open (or the wrong variable) |
|
||||
| `OUT OF DATA` | `READ` after the last `DATA` item |
|
||||
| `SUBSCRIPT OUT OF RANGE` | An array subscript below 0 or beyond its `DIM` |
|
||||
| `DIVISION BY ZERO` | Dividing by 0 |
|
||||
| `DIVISION BY ZERO` | Dividing by 0, or `MOD(A, 0)` |
|
||||
| `OUT OF MEMORY` | The program and arrays don't fit |
|
||||
| `TYPE MISMATCH` | A string where a number belongs, or the other way round |
|
||||
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store (also from `RENUM`) |
|
||||
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
|
||||
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, `ASIN(2)`, `FACT(-1)` (section 6), a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
|
||||
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
|
||||
| `FOR NESTED TOO DEEPLY` | More than 16 loops open |
|
||||
| `FOR NESTED TOO DEEPLY` | More than 16 `FOR` loops open |
|
||||
| `DO NESTED TOO DEEPLY` | More than 16 `DO` loops open |
|
||||
| `FOR WITHOUT NEXT` | A `FOR` that runs no times, or `EXIT FOR`, with no `NEXT` after it |
|
||||
| `DO WITHOUT LOOP` | A `DO` whose condition fails, or `EXIT DO`, with no `LOOP` after it |
|
||||
| `LOOP WITHOUT DO` | `LOOP` or `EXIT DO` with no `DO` loop open |
|
||||
| `IF WITHOUT END IF` | A block `IF` (or its `ELSE`) with no `END IF` after it |
|
||||
| `SELECT WITHOUT END SELECT` | A `SELECT CASE` (or its `CASE`) with no `END SELECT` after it |
|
||||
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
|
||||
| `FILE NOT FOUND` | `LOAD`, `RUN` or `DEL` of a name not in `DIR` |
|
||||
| `BAD FILE NAME` | A name that is empty, longer than 8 or not just letters and digits |
|
||||
|
||||
Binary file not shown.
@@ -5,7 +5,7 @@
|
||||
python3 test-cavern.py [--shots DIR] [--play]
|
||||
|
||||
It checks the title screen, measures the Z-80 instructions each frame takes
|
||||
(a 60 Hz frame has about 550,000 at 90 MHz), checks that walls stop the miner,
|
||||
(a 60 Hz frame has about 610,000 at 100 MHz), checks that walls stop the miner,
|
||||
that crystals score and that the bats leave the roost, and with --play lets
|
||||
an autopilot gather every crystal in the cave by breadth-first search. The
|
||||
autopilot can't be caught (it keeps the miner shielded), so this checks the
|
||||
|
||||
@@ -5,7 +5,7 @@ and take screenshots.
|
||||
python3 test-raid.py [--shots DIR] [--play]
|
||||
|
||||
It checks the title screen, measures the Z-80 instructions each frame takes
|
||||
(a 60 Hz frame has about 550,000 at 90 MHz), flies into the fortress, and with
|
||||
(a 60 Hz frame has about 610,000 at 100 MHz), flies into the fortress, and with
|
||||
--play lets an autopilot fly the whole level: it steers through every doorway
|
||||
at the right height, climbs over low walls and shoots ahead. The autopilot
|
||||
can't be hit by shells or fighters (it keeps the ship invulnerable), so this
|
||||
@@ -57,7 +57,7 @@ def main():
|
||||
c0 = b.steps
|
||||
b.run_frames(60)
|
||||
per = (b.steps - c0) / 60
|
||||
print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 90 MHz)" % (per, per / 5500))
|
||||
print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 100 MHz)" % (per, per / 6100))
|
||||
check("a frame fits in a 60th of a second", per < 500000)
|
||||
if a.shots: b.shot(os.path.join(a.shots, "2-approach.png"), font)
|
||||
alt0 = b.m[sym["_alt"]]
|
||||
|
||||
@@ -5,7 +5,7 @@ and take screenshots.
|
||||
python3 test-swarm.py [--shots DIR] [--play]
|
||||
|
||||
It checks the title screen, measures the Z-80 instructions each frame takes
|
||||
(a 60 Hz frame has about 550,000 at 90 MHz), checks that the formation sways
|
||||
(a 60 Hz frame has about 610,000 at 100 MHz), checks that the formation sways
|
||||
and that aliens dive, shoots one out of the formation, and with --play lets an
|
||||
autopilot clear the whole swarm. The autopilot can't be killed (it keeps the
|
||||
ship shielded), so this checks the rules and the waves, not its skill.
|
||||
|
||||
@@ -5,7 +5,7 @@ and take screenshots.
|
||||
python3 test-warden.py [--shots DIR] [--play]
|
||||
|
||||
It checks the title screen, measures the Z-80 instructions each frame takes
|
||||
(a 60 Hz frame has about 550,000 at 90 MHz), flies along the planet checking
|
||||
(a 60 Hz frame has about 610,000 at 100 MHz), flies along the planet checking
|
||||
that the picture scrolls and the scanner fills in, and with --play lets an
|
||||
autopilot hunt down every enemy until the wave is cleared. The autopilot
|
||||
can't be killed (it keeps the ship shielded), so this checks the rules and the
|
||||
|
||||
+133
-1
@@ -1,5 +1,6 @@
|
||||
/* Single-precision maths for BASIC: sinf cosf tanf atanf expf logf sqrtf
|
||||
floorf. Range reduction by Cody and Waite, minimax polynomials as in the
|
||||
floorf, and from them asinf acosf atan2f sinhf coshf tanhf asinhf acoshf
|
||||
atanhf log1pf log10f log2f and an exact fmodf. Range reduction by Cody and Waite, minimax polynomials as in the
|
||||
Cephes library; good to an ulp or two, which BASIC's 6-7 printed digits
|
||||
don't show. sqrtf (the FPU's FSQRT.S when there is one) and floorf are
|
||||
exact. (newlib's versions are larger and
|
||||
@@ -190,3 +191,134 @@ float floorf(float x)
|
||||
else if (u.i << 1) u.f = -1.0f;
|
||||
return u.f;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- built on the above */
|
||||
static const float PI_F = 3.14159265358979f, LN2_F = 0.693147180559945f;
|
||||
|
||||
/* log(1 + u), accurate for small u: Goldberg's trick, since w - 1 is exact */
|
||||
float log1pf(float u)
|
||||
{
|
||||
float w = 1.0f + u;
|
||||
if (w == 1.0f) return u;
|
||||
return logf(w) * (u / (w - 1.0f));
|
||||
}
|
||||
|
||||
float asinf(float x) /* |x| <= 1 */
|
||||
{
|
||||
return atanf(x / sqrtf((1.0f - x) * (1.0f + x)));
|
||||
}
|
||||
|
||||
float acosf(float x) /* |x| <= 1 */
|
||||
{
|
||||
return 2.0f * atanf(sqrtf((1.0f - x) / (1.0f + x)));
|
||||
}
|
||||
|
||||
float atan2f(float y, float x)
|
||||
{
|
||||
float a;
|
||||
if (x == 0.0f) return y > 0 ? PI_F / 2 : y < 0 ? -PI_F / 2 : 0.0f;
|
||||
a = atanf(y / x);
|
||||
if (x < 0) a += y < 0 ? -PI_F : PI_F;
|
||||
return a;
|
||||
}
|
||||
|
||||
/* e^|x| / 2 for large |x|, without overflowing on the way */
|
||||
static float half_exp(float a)
|
||||
{
|
||||
float h = expf(0.5f * a);
|
||||
return (0.5f * h) * h;
|
||||
}
|
||||
|
||||
float sinhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r, z, e;
|
||||
if (a < 1.0f) { /* the series: no cancellation */
|
||||
z = a * a;
|
||||
r = a + a * z * (1.6666667163e-1f + z * (8.3333337680e-3f + z * (1.9841270114e-4f
|
||||
+ z * (2.7557318845e-6f + z * 2.5052108385e-8f))));
|
||||
} else if (a < 10.0f) {
|
||||
e = expf(a);
|
||||
r = 0.5f * (e - 1.0f / e);
|
||||
} else r = half_exp(a);
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float coshf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, e;
|
||||
if (a >= 10.0f) return half_exp(a);
|
||||
e = expf(a);
|
||||
return 0.5f * (e + 1.0f / e);
|
||||
}
|
||||
|
||||
float tanhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
r = a >= 10.0f ? 1.0f : sinhf(a) / coshf(a);
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float asinhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
if (a > 1e9f) r = logf(a) + LN2_F;
|
||||
else r = log1pf(a + a * a / (1.0f + sqrtf(1.0f + a * a)));
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float acoshf(float x) /* x >= 1 */
|
||||
{
|
||||
float t;
|
||||
if (x > 1e9f) return logf(x) + LN2_F;
|
||||
t = x - 1.0f;
|
||||
return log1pf(t + sqrtf(2.0f * t + t * t));
|
||||
}
|
||||
|
||||
float atanhf(float x) /* |x| < 1 */
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
r = 0.5f * log1pf(2.0f * a / (1.0f - a));
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
/* log of x = m * 2^e, m in [sqrt(1/2), sqrt(2)): returns log(m), sets e */
|
||||
static float log_split(float x, int *e)
|
||||
{
|
||||
fbits u = { x };
|
||||
int k = 0;
|
||||
if (u.i < 0x00800000) { u.f *= 16777216.0f; k = -24; } /* subnormal */
|
||||
k += (int)(u.i >> 23) - 127;
|
||||
u.i = (u.i & 0x007FFFFF) | 0x3F800000; /* [1, 2) */
|
||||
if (u.f > 1.41421356f) { u.f *= 0.5f; k++; }
|
||||
*e = k;
|
||||
return logf(u.f);
|
||||
}
|
||||
|
||||
float log2f(float x) /* x > 0: exact for powers of 2 */
|
||||
{
|
||||
int e;
|
||||
float y = log_split(x, &e);
|
||||
return y * 1.44269504088896341f + (float)e;
|
||||
}
|
||||
|
||||
float log10f(float x) /* x > 0 */
|
||||
{
|
||||
int e;
|
||||
float y = log_split(x, &e), f = (float)e;
|
||||
/* log10(2) and log10(e) each split in two, as in Cephes */
|
||||
return ((f * 2.48745663981195213739e-4f + y * 7.00731903251827651129e-4f)
|
||||
+ y * 4.3359375e-1f) + f * 3.0078125e-1f;
|
||||
}
|
||||
|
||||
/* exactly x - n*y, n = x/y truncated, by binary long division: each step
|
||||
subtracts t from r with t <= r < 2t, which is exact */
|
||||
float fmodf(float x, float y)
|
||||
{
|
||||
float r = x < 0 ? -x : x, d = y < 0 ? -y : y, t;
|
||||
if (d == 0.0f || r < d) return x;
|
||||
t = d;
|
||||
while (t <= 0.5f * r) t += t;
|
||||
for (; t >= d; t *= 0.5f)
|
||||
if (r >= t) r -= t;
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
// Basys 3 VT220 terminal with ANSI colour, and a RISC-V computer running BASIC.
|
||||
// SW15 down: keyboard (USB HID port) -> UART TX (USB-UART port),
|
||||
// UART RX -> 80x30 colour text on VGA
|
||||
// UART RX -> 132x64 (or 80x32) colour text on VGA
|
||||
// SW15 up: the terminal is the computer's console
|
||||
//
|
||||
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1)
|
||||
@@ -32,9 +32,9 @@ module basys3_terminal #(
|
||||
parameter MON_FILE = "monitor.mem",
|
||||
parameter WIDE_BOOT = 1, // the screen starts at 132x64 (0: 80x32)
|
||||
parameter CLK_HZ = 100_000_000,
|
||||
parameter real CPU_MULT = 9.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
|
||||
parameter real CPU_MULT = 10.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
|
||||
parameter real CPU_DIV = 10.0,
|
||||
parameter CPU_HZ = 90_000_000,
|
||||
parameter CPU_HZ = 100_000_000,
|
||||
parameter ROM_TOP = 32'h0000_A000 // BASIC's code is below; writes there are ignored
|
||||
) (
|
||||
input clk,
|
||||
@@ -247,10 +247,11 @@ module basys3_terminal #(
|
||||
// Start-up banner, fed in like received text. Bytes are read from the
|
||||
// high end of the string; the unused high bytes are zero and skipped.
|
||||
localparam [8*200-1:0] BANNER = {
|
||||
"\033[1mBasys 3 VT220 Terminal\033[0m 80x32 ",
|
||||
"\033[1mBasys 3 VT220 Terminal\033[0m ", WIDE_BOOT ? "132x64" : "80x32 ", " ",
|
||||
"\033[31mc\033[33mo\033[32ml\033[36mo\033[34mu\033[35mr\033[0m ",
|
||||
"\033[4munderline\033[0m\015\012",
|
||||
"UART RX -> screen, keyboard -> UART TX TERM=basys3 or vt220\015\012\012"};
|
||||
"UART RX -> screen, keyboard -> UART TX TERM=vt220 or ",
|
||||
WIDE_BOOT ? "basys3-w" : "basys3 ", "\015\012\012"};
|
||||
// (not shown in computer mode, where BASIC prints its own)
|
||||
reg [7:0] ban_i;
|
||||
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
|
||||
|
||||
+4
-4
@@ -8,9 +8,9 @@
|
||||
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
|
||||
// the bus waits), multiplies 4 and divides 35. Floating-point operations run
|
||||
// in rv32_fpu.v: 4 clocks for moves, sign injection, compares, min/max and
|
||||
// class, 5 for float -> int, 8 for int -> float, 12 for add, subtract,
|
||||
// class, 5 for float -> int, 9 for int -> float, 13 for add, subtract,
|
||||
// multiply and the multiply-adds (4 if an operand is infinite, NaN or zero),
|
||||
// 36 for divide and square root; float loads and stores as integer ones.
|
||||
// 37 for divide and square root; float loads and stores as integer ones.
|
||||
//
|
||||
// Instruction port: synchronous read. i_rdata is the word at the i_addr given
|
||||
// in the last cycle with i_en high, and must hold while i_en is low.
|
||||
@@ -29,8 +29,8 @@
|
||||
// breakpoint) are raised one cycle late, from registers, to keep the long
|
||||
// paths out of the trap logic; the instruction has no effect either way.
|
||||
//
|
||||
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
|
||||
// LUTs, 900 flip-flops and 4 DSP slices.
|
||||
// Speed: 100 MHz in the full design on the XC7A35T-1. The integer core
|
||||
// alone (bench/) is about 2100 LUTs, 900 flip-flops and 4 DSP slices.
|
||||
//
|
||||
// Implemented: RV32I, M, F (rv32_fpu.v; fflags, frm, fcsr; mstatus.FS, which
|
||||
// resets to Initial: F instructions and CSRs are illegal while it is Off,
|
||||
|
||||
+47
-21
@@ -20,9 +20,11 @@
|
||||
// The rest (sign injection, min/max, compares, class, moves, float -> int)
|
||||
// finish without it.
|
||||
//
|
||||
// Clocks from start to done: multiply-add family 10 (2 when an operand is
|
||||
// infinite, NaN or zero), divide and square root 34, int -> float 6,
|
||||
// float -> int 3, the rest 2.
|
||||
// Clocks from start to done: multiply-add family 11 (2 when an operand is
|
||||
// infinite, NaN or zero), divide and square root 35, int -> float 7,
|
||||
// float -> int 3, the rest 2. (Exponents, leading zeros and the FADD/FSUB
|
||||
// operand swap are worked out as the operands are taken, and the back end
|
||||
// finds the leading one a clock before using it, for 100 MHz.)
|
||||
module rv32_fpu (
|
||||
input clk,
|
||||
input rst,
|
||||
@@ -40,7 +42,7 @@ module rv32_fpu (
|
||||
localparam S_IDLE = 4'd0, S_U = 4'd1, S_M1 = 4'd2, S_M2 = 4'd3, S_ADD = 4'd4,
|
||||
S_ABS = 4'd5, S_N1 = 4'd6, S_N2 = 4'd7, S_R1 = 4'd8, S_R2 = 4'd9,
|
||||
S_DIV = 4'd10, S_SQRT = 4'd11, S_CW = 4'd12, S_DIVF = 4'd13,
|
||||
S_SQRTF = 4'd14;
|
||||
S_SQRTF = 4'd14, S_NZ = 4'd15;
|
||||
reg [3:0] st;
|
||||
|
||||
// ------------------------------------------------------------ operands
|
||||
@@ -59,10 +61,12 @@ module rv32_fpu (
|
||||
wire op_fma = r4 || op_add || op_mul;
|
||||
wire uns = ri[20];
|
||||
|
||||
// a (for FADD/FSUB the multiplier is 1.0), b, c
|
||||
// a (for FADD/FSUB the multiplier is 1.0), b, c: picked, with their
|
||||
// exponents and leading zeros, as the operands arrive (see S_IDLE)
|
||||
reg [31:0] rxb, rxc;
|
||||
wire [31:0] xa = ra;
|
||||
wire [31:0] xb = op_add ? 32'h3F80_0000 : rb;
|
||||
wire [31:0] xc = op_add ? rb : rc;
|
||||
wire [31:0] xb = rxb;
|
||||
wire [31:0] xc = rxc;
|
||||
wire negp = r4 && opc[3];
|
||||
wire negc = (r4 && opc[2]) || (op_add && f5[0]);
|
||||
wire use_c = !op_mul;
|
||||
@@ -80,9 +84,8 @@ module rv32_fpu (
|
||||
wire [23:0] ma = {ex8(xa) != 8'd0, xa[22:0]};
|
||||
wire [23:0] mb = {ex8(xb) != 8'd0, xb[22:0]};
|
||||
wire [23:0] mc = {ex8(xc) != 8'd0, xc[22:0]};
|
||||
wire signed [11:0] ea = ex8(xa) == 8'd0 ? 12'sd1 : {4'd0, ex8(xa)};
|
||||
wire signed [11:0] eb = ex8(xb) == 8'd0 ? 12'sd1 : {4'd0, ex8(xb)};
|
||||
wire signed [11:0] ec = ex8(xc) == 8'd0 ? 12'sd1 : {4'd0, ex8(xc)};
|
||||
reg signed [11:0] ea, eb, ec; // exponents (1 for a subnormal)
|
||||
reg signed [11:0] eab; // ea + eb - 127, the product's
|
||||
wire sp = xa[31] ^ xb[31] ^ negp;
|
||||
wire sc = xc[31] ^ negc;
|
||||
|
||||
@@ -158,7 +161,7 @@ module rv32_fpu (
|
||||
// sits at the top and the exponent comes from c (the product only adds
|
||||
// stickiness, which rounds the same at any distance that far down).
|
||||
// (worked out from the operands in S_U, used in S_M1)
|
||||
wire signed [12:0] s_u = {ec[11], ec} - ({ea[11], ea} + {eb[11], eb} - 13'sd127) + 13'sd23;
|
||||
wire signed [12:0] s_u = {ec[11], ec} - {eab[11], eab} + 13'sd23;
|
||||
reg signed [12:0] s_amt;
|
||||
reg s_big;
|
||||
reg [76:0] c_sh;
|
||||
@@ -191,7 +194,8 @@ module rv32_fpu (
|
||||
reg n_zero;
|
||||
reg [76:0] y;
|
||||
reg y_st; // sticky from a right shift
|
||||
wire signed [11:0] e_norm = n_ex - {5'd0, lz};
|
||||
reg [6:0] n_lz; // lz, registered in S_NZ
|
||||
reg signed [11:0] e_norm; // n_ex - lz
|
||||
reg signed [11:0] r_e; // the result's exponent (1 if subnormal)
|
||||
// rounding
|
||||
wire [23:0] y_m = y[76:53];
|
||||
@@ -229,9 +233,19 @@ module rv32_fpu (
|
||||
for (i = 0; i < 24; i = i + 1) if (m[i]) lz24 = 5'd23 - i[4:0];
|
||||
end
|
||||
endfunction
|
||||
wire [4:0] lza = lz24(ma), lzb = lz24(mb);
|
||||
reg [4:0] lza, lzb;
|
||||
wire [23:0] na = ma << lza, nb = mb << lzb;
|
||||
wire signed [11:0] nea = ea - {7'd0, lza}, neb = eb - {7'd0, lzb};
|
||||
reg signed [11:0] nea, neb; // exponents of na, nb
|
||||
|
||||
// the same from the operands as they arrive
|
||||
function signed [11:0] exb(input [31:0] x);
|
||||
exb = x[30:23] == 8'd0 ? 12'sd1 : {4'd0, x[30:23]};
|
||||
endfunction
|
||||
function [23:0] mant(input [31:0] x); mant = {x[30:23] != 8'd0, x[22:0]}; endfunction
|
||||
wire i_add = ins[6:4] != 3'b100 && ins[31:28] == 4'h0; // FADD, FSUB
|
||||
wire [31:0] i_b = i_add ? 32'h3F80_0000 : b;
|
||||
wire [31:0] i_c = i_add ? b : c;
|
||||
wire [4:0] i_lza = lz24(mant(a)), i_lzb = lz24(mant(i_b));
|
||||
|
||||
// ------------------------------------------------------- float -> int
|
||||
reg [32:0] cw_int;
|
||||
@@ -246,13 +260,19 @@ module rv32_fpu (
|
||||
case (st)
|
||||
S_IDLE: if (start) begin
|
||||
ra <= a; rb <= b; rc <= c; ri <= ins; rrm <= rm;
|
||||
rxb <= i_b; rxc <= i_c;
|
||||
ea <= exb(a); eb <= exb(i_b); ec <= exb(i_c);
|
||||
eab <= exb(a) + exb(i_b) - 12'sd127;
|
||||
lza <= i_lza; lzb <= i_lzb;
|
||||
nea <= exb(a) - {7'd0, i_lza};
|
||||
neb <= exb(i_b) - {7'd0, i_lzb};
|
||||
st <= S_U;
|
||||
end
|
||||
|
||||
S_U: begin
|
||||
// multiply-add specials (only used by the multiply-add family)
|
||||
pa <= ma; pb <= mb;
|
||||
ep <= ea + eb - 12'sd127;
|
||||
ep <= eab;
|
||||
c_m <= mc; c_e <= ec; c_zero <= cz || !use_c;
|
||||
s_big <= !(cz || !use_c) && s_u > 13'sd50;
|
||||
s_amt <= (!(cz || !use_c) && s_u > 13'sd50) ? 13'sd50 : s_u;
|
||||
@@ -315,7 +335,7 @@ module rv32_fpu (
|
||||
n_sign <= !uns && ra[31];
|
||||
n_x <= {(!uns && ra[31] ? -ra : ra), 45'd0};
|
||||
n_ex <= 12'sd158;
|
||||
st <= S_N1;
|
||||
st <= S_NZ;
|
||||
end else if (op_cvtw) begin
|
||||
cw_neg <= ra[31];
|
||||
if (ex8(ra) >= 8'd150) begin
|
||||
@@ -373,17 +393,23 @@ module rv32_fpu (
|
||||
n_sign <= noadd ? s_p : s_c;
|
||||
end
|
||||
n_ex <= fx;
|
||||
st <= S_NZ;
|
||||
end
|
||||
|
||||
S_NZ: begin // find the top bit
|
||||
n_zero <= n_x == 77'd0;
|
||||
n_lz <= lz;
|
||||
e_norm <= n_ex - {5'd0, lz};
|
||||
st <= S_N1;
|
||||
end
|
||||
|
||||
S_N1: begin // where to put the top bit
|
||||
n_zero <= n_x == 77'd0;
|
||||
S_N1: begin // where to put it
|
||||
if (e_norm < 12'sd1) begin
|
||||
n_sh <= n_ex - 12'sd1; // subnormal: as far as exponent 1
|
||||
r_e <= 12'sd1;
|
||||
n_tiny <= 1'b1;
|
||||
end else begin
|
||||
n_sh <= {5'd0, lz};
|
||||
n_sh <= {5'd0, n_lz};
|
||||
r_e <= e_norm;
|
||||
n_tiny <= 1'b0;
|
||||
end
|
||||
@@ -455,12 +481,12 @@ module rv32_fpu (
|
||||
|
||||
S_DIVF: begin
|
||||
n_x <= {dq, drem != 26'd0, 49'd0};
|
||||
st <= S_N1;
|
||||
st <= S_NZ;
|
||||
end
|
||||
|
||||
S_SQRTF: begin
|
||||
n_x <= {dq, srem != 29'd0, 49'd0};
|
||||
st <= S_N1;
|
||||
st <= S_NZ;
|
||||
end
|
||||
|
||||
default: st <= S_IDLE;
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ module rv32_system #(
|
||||
parameter MEM_FILE = "basic.mem", // RAM 0000-FFFF
|
||||
parameter MEM_HI_FILE = "basic-hi.mem", // RAM 10000-13FFF
|
||||
parameter MON_FILE = "monitor.mem",
|
||||
parameter CPU_HZ = 90_000_000,
|
||||
parameter CPU_HZ = 100_000_000,
|
||||
parameter ROM_TOP = 32'h0000_A000
|
||||
) (
|
||||
input clk, // CPU clock
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@
|
||||
// sigma-delta modulator at the full clock rate; the PmodAMP2 filters it and
|
||||
// drives headphones or a speaker.
|
||||
module sound #(
|
||||
parameter CPU_HZ = 90_000_000
|
||||
parameter CPU_HZ = 100_000_000
|
||||
) (
|
||||
input clk,
|
||||
input rst,
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ set root [file normalize [file dirname [info script]]/..]
|
||||
set out $root/build
|
||||
file mkdir $out
|
||||
|
||||
set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 9.0}]
|
||||
set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 10.0}]
|
||||
set div [expr {[info exists ::env(CPU_DIV)] ? $::env(CPU_DIV) : 10.0}]
|
||||
set hz [expr {int(round(100e6 * $mult / $div))}]
|
||||
puts "CPU clock: [expr {$hz / 1e6}] MHz"
|
||||
|
||||
@@ -264,6 +264,8 @@ module tb_terminal;
|
||||
|
||||
initial begin
|
||||
#2_000_000; // reset, clear, banner
|
||||
if (WIDE) expect_row(0, "Basys 3 VT220 Terminal 132x64");
|
||||
else expect_row(0, "Basys 3 VT220 Terminal 80x32");
|
||||
|
||||
`ifndef SKIP_TERMINAL
|
||||
// ---- keyboard: a, shift+b, Enter, up, ctrl+c, F5
|
||||
|
||||
+2
-2
@@ -559,10 +559,10 @@ class RV32:
|
||||
if op in (0x07, 0x27):
|
||||
return 3
|
||||
if op in (0x43, 0x47, 0x4B, 0x4F):
|
||||
return 12
|
||||
return 13
|
||||
if op == 0x53:
|
||||
f5 = ins >> 27
|
||||
return {0: 12, 1: 12, 2: 12, 3: 36, 0x0B: 36, 0x18: 5, 0x1A: 8}.get(f5, 4)
|
||||
return {0: 13, 1: 13, 2: 13, 3: 37, 0x0B: 37, 0x18: 5, 0x1A: 9}.get(f5, 4)
|
||||
if op in (0x03, 0x23):
|
||||
a = (self.x[ins >> 15 & 31] + sx(ins >> 20 if op == 3 else (ins >> 25) << 5 | (ins >> 7 & 31), 12)) & M32
|
||||
return 4 if a >= 0xFFFFFE00 or MON_RAM <= a < MON_END else 3
|
||||
|
||||
Reference in new issue
Block a user