1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-26 07:18:05 +11:00

Compare commits

..

8 Commits

Author SHA1 Message Date
Virgil Dupras
7410891ad1 lib/expr: fix unary minus
For some reason, I've mistakenly disabled tests in test_expr without noticing
and I also broke "-123" parsing. Fixed.
2019-12-23 20:53:31 -05:00
Virgil Dupras
6d88c3a754 parseExprDE --> parseExpr 2019-12-23 19:13:44 -05:00
Virgil Dupras
5301200d6f basic: parseExpr --> parseExprDE 2019-12-23 19:01:03 -05:00
Virgil Dupras
476178ee7c zasm: parseExpr --> parseExprDE 2019-12-23 18:44:55 -05:00
Virgil Dupras
025b90909f Update bootstrap bin 2019-12-23 16:03:23 -05:00
Virgil Dupras
aef96c5e96 Add missing common.asm test harness file 2019-12-23 16:02:26 -05:00
Virgil Dupras
cc754e12aa parseExpr --> parseExprDE: easy ones
Those replacements were trivially equivalent. For the other ones, an
examination of the context is necessary.
2019-12-23 15:59:55 -05:00
Virgil Dupras
a034f63e23 test: begin adding common test harnessing code
This should make tests a bit more convenient to write and debug.

Moreover, begin de de-IX-ization of parseExpr. I have, in a local WIP, a
parseExpr implemented using a recursive descent algo, it passes all tests, but
it unfortunately assembles a faulty zasm. I have to find the expressions that
it doesn't parse properly.

But before I do that, I prefer to commit these significant improvements I've
been making to tests harness in parallel of this development.
2019-12-23 15:41:25 -05:00
16 changed files with 230 additions and 219 deletions

View File

@ -224,7 +224,6 @@ basPRINT:
ex de, hl
call parseExpr
jr nz, .parseError
push ix \ pop de
ld hl, SCRATCHPAD
call fmtDecimalS
call printstr
@ -254,7 +253,6 @@ basGOTO:
ex de, hl
call parseExpr
ret nz
push ix \ pop de
call bufFind
jr nz, .notFound
push ix \ pop de
@ -317,7 +315,7 @@ basINPUT:
call rdSep
call stdioReadLine
call parseExpr
ld (VAR_TBL), ix
ld (VAR_TBL), de
call printcrlf
cp a ; ensure Z
ret

View File

@ -42,7 +42,6 @@ parseTruth:
.simple:
call parseExpr
jr nz, .end
push ix \ pop de
ld a, d
or e
jr .success
@ -132,12 +131,12 @@ parseTruth:
; place their corresponding values in HL and DE.
.parseLeftRight:
; let's start with HL
push de ; --> lvl 1
call parseExpr
pop hl ; <-- lvl 1, orig DE
ret nz
push ix ; --> lvl 1. save (HL) value in stack.
ex de, hl
call parseExpr
ret nz
push ix \ pop de
pop hl ; <-- lvl 1. restore.
push de ; --> lvl 1. save HL value in stack.
; Now, for DE. (DE) is now in HL
call parseExpr ; DE in place
pop hl ; <-- lvl 1. restore saved HL
ret

View File

@ -85,11 +85,13 @@ rdWord:
; Read word from HL in SCRATCHPAD and then intepret that word as an expression.
; Put the result in IX.
; Z for success.
; TODO: put result in DE
rdExpr:
ld de, SCRATCHPAD
call rdWord
push hl
ex de, hl
call parseExpr
push de \ pop ix
pop hl
ret

View File

@ -52,10 +52,9 @@ varTryAssign:
call rdWord
ex de, hl
; Now, evaluate that expression now in (HL)
call parseExpr ; --> number in IX
call parseExpr ; --> number in DE
jr nz, .exprErr
pop af ; <-- lvl 4
push ix \ pop de ; send number to DE
call varAssign
xor a ; ensure Z
.end:

View File

@ -11,19 +11,18 @@
;
; *** Code ***
;
; Parse expression in string at (HL) and returns the result in IX.
; Parse expression in string at (HL) and returns the result in DE.
; **This routine mutates (HL).**
; We expect (HL) to be disposable: we mutate it to avoid having to make a copy.
; Sets Z on success, unset on error.
; TODO: the IX output register is a bit awkward. Nearly everywhere, I need
; to push \ pop that thing. See if we could return the result in DE
; instead.
parseExpr:
push de
push ix
push hl
call _parseExpr
push ix \ pop de
pop hl
pop de
pop ix
ret
_parseExpr:
@ -97,25 +96,29 @@ _findAndSplit:
; parse expression on the left (HL) and the right (DE) and put the results in
; HL (left) and DE (right)
_resolveLeftAndRight:
; special case: is (HL) zero? If yes, it means that our left operand
; is empty. consider it as 0
ld ix, 0 ; pre-set to 0
ld a, (hl)
or a
jr z, .skip
jr z, .noleft
; Parse left operand in (HL)
push de ; --> lvl 1
call parseExpr
pop hl ; <-- lvl 1, orig DE
ret nz ; return immediately if error
.skip:
.parseright:
; Now we have parsed everything to the left and we have its result in
; IX. What we need to do now is the same thing on (DE) and then apply
; the + operator. Let's save IX somewhere and parse this.
ex de, hl ; right expr now in HL
push ix ; --> lvl 1
call parseExpr
pop hl ; <-- lvl 1. left
push ix \ pop de ; right
; DE. What we need to do now is the same thing on (DE) and then apply
; the + operator. Let's save DE somewhere and parse this.
push de ; --> lvl 1
; right expr in (HL)
call parseExpr ; DE is set
pop hl ; <-- lvl 1. left value
ret ; Z is parseExpr's result
.noleft:
; special case: is (HL) zero? If yes, it means that our left operand
; is empty. consider it as 0
ex de, hl ; (DE) goes in (HL) for .parseright
ld de, 0
jr .parseright
; Routines in here all have the same signature: they take two numbers, DE (left)
; and IX (right), apply the operator and put the resulting number in DE.

View File

@ -643,14 +643,15 @@ _readK8:
jr _readExpr
_readDouble:
push ix
push de
call parseExpr
jr nz, .end
push ix \ pop bc
ld b, d
ld c, e
; BC is already set. For good measure, let's set A to BC's MSB
ld a, b
.end:
pop ix
pop de
ret
_readk7:
@ -663,13 +664,12 @@ _readk7:
; truncation checks might falsely fail.
call zasmIsFirstPass
jr z, .end
; IX contains an absolute value. Turn this into a -64/+63 relative
; DE contains an absolute value. Turn this into a -64/+63 relative
; value by subtracting PC from it. However, before we do that, let's
; add 0x7f to it, which we'll remove later. This will simplify bounds
; checks. (we use 7f instead of 3f because we deal in bytes here, not
; in words)
push ix \ pop hl
ld de, 0x7f
ld hl, 0x7f
add hl, de ; Carry cleared
ex de, hl
call zasmGetPC ; --> HL
@ -706,6 +706,7 @@ _readR4:
; read a rXX argument and return register number in A.
; Set Z for success.
_readR5:
push de
push ix
ld a, (hl)
call upcase
@ -714,43 +715,44 @@ _readR5:
inc hl
call parseDecimal
jr nz, .end
push ix \ pop de
ld a, 31
call _IX2A
call _DE2A
.end:
pop ix
pop de
ret
; Put IX's LSB into A and, additionally, ensure that the new value is <=
; Put DE's LSB into A and, additionally, ensure that the new value is <=
; than what was previously in A.
; Z for success.
_IX2A:
push ix \ pop hl
cp l
jp c, unsetZ ; A < L
ld a, h
_DE2A:
cp e
jp c, unsetZ ; A < E
ld a, d
or a
ret nz ; should be zero
ld a, l
ld a, e
; Z set from "or a"
ret
; Read expr and return success only if result in under number given in A
; Z for success
_readExpr:
push ix
push de
push bc
ld b, a
call parseExpr
jr nz, .end
ld a, b
call _IX2A
call _DE2A
jr nz, .end
or c
ld c, a
cp a ; ensure Z
.end:
pop bc
pop ix
pop de
ret
; Parse one of the following: X, Y, Z, X+, Y+, Z+, -X, -Y, -Z.

View File

@ -40,6 +40,7 @@ dirHandlers:
.dw handleBIN
handleDB:
push de
push hl
.loop:
call readWord
@ -49,18 +50,19 @@ handleDB:
jr z, .stringLiteral
call parseExpr
jr nz, .badarg
push ix \ pop hl
ld a, h
ld a, d
or a ; cp 0
jr nz, .overflow ; not zero? overflow
ld a, l
ld a, e
call ioPutB
jr nz, .ioError
.stopStrLit:
call readComma
jr z, .loop
cp a ; ensure Z
.end:
pop hl
pop de
ret
.ioError:
ld a, SHELL_ERR_IO_ERROR
@ -74,9 +76,8 @@ handleDB:
.overflow:
ld a, ERR_OVFL
.error:
call unsetZ
pop hl
ret
or a ; unset Z
jr .end
.stringLiteral:
ld a, (hl)
@ -89,6 +90,7 @@ handleDB:
jr .stringLiteral
handleDW:
push de
push hl
.loop:
call readWord
@ -96,17 +98,18 @@ handleDW:
ld hl, scratchpad
call parseExpr
jr nz, .badarg
push ix \ pop hl
ld a, l
ld a, e
call ioPutB
jr nz, .ioError
ld a, h
ld a, d
call ioPutB
jr nz, .ioError
call readComma
jr z, .loop
cp a ; ensure Z
.end:
pop hl
pop de
ret
.ioError:
ld a, SHELL_ERR_IO_ERROR
@ -117,9 +120,8 @@ handleDW:
.badarg:
ld a, ERR_BAD_ARG
.error:
call unsetZ
pop hl
ret
or a ; unset Z
jr .end
handleEQU:
call zasmIsLocalPass ; Are we in local pass? Then ignore all .equ.
@ -149,7 +151,6 @@ handleEQU:
call parseExpr
jr nz, .badarg
ld hl, DIREC_SCRATCHPAD
push ix \ pop de
; Save value in "@" special variable
ld (DIREC_LASTVAL), de
call symRegisterConst ; A and Z set
@ -179,14 +180,17 @@ handleEQU:
jp readWord
handleORG:
push de
call readWord
jr nz, .badfmt
call parseExpr
jr nz, .badarg
push ix \ pop hl
ex de, hl
ld (DIREC_LASTVAL), hl
call zasmSetOrg
cp a ; ensure Z
.end:
pop de
ret
.badfmt:
ld a, ERR_BAD_FMT
@ -194,31 +198,28 @@ handleORG:
.badarg:
ld a, ERR_BAD_ARG
.error:
call unsetZ
ret
or a ; unset Z
jr .end
handleFIL:
call readWord
jr nz, .badfmt
call parseExpr
jr nz, .badarg
push bc ; --> lvl 1
push ix \ pop bc
ld a, b
ld a, d
cp 0xd0
jr nc, .overflow
.loop:
ld a, b
or c
ld a, d
or e
jr z, .loopend
xor a
call ioPutB
jr nz, .ioError
dec bc
dec de
jr .loop
.loopend:
cp a ; ensure Z
pop bc ; <-- lvl 1
ret
.ioError:
ld a, SHELL_ERR_IO_ERROR
@ -230,11 +231,11 @@ handleFIL:
ld a, ERR_BAD_ARG
jp unsetZ
.overflow:
pop bc ; <-- lvl 1
ld a, ERR_OVFL
jp unsetZ
handleOUT:
push de
push hl
; Read our expression
call readWord
@ -244,10 +245,9 @@ handleOUT:
ld hl, scratchpad
call parseExpr
jr nz, .badarg
push ix \ pop hl
ld a, h
ld a, d
out (ZASM_DEBUG_PORT), a
ld a, l
ld a, e
out (ZASM_DEBUG_PORT), a
jr .end
.badfmt:
@ -256,9 +256,10 @@ handleOUT:
.badarg:
ld a, ERR_BAD_ARG
.error:
call unsetZ
or a ; unset Z
.end:
pop hl
pop de
ret
handleINC:

View File

@ -146,7 +146,7 @@ parseIXY:
; any argspec (A == 0 means arg is empty). A return value of 0xff means an
; error.
;
; If the parsed argument is a number constant, 'N' is returned and IX contains
; If the parsed argument is a number constant, 'N' is returned and DE contains
; the value of that constant.
parseArg:
call strlen
@ -154,13 +154,8 @@ parseArg:
ret z ; empty string? A already has our result: 0
push bc
push de
push hl
; We always initialize IX to zero so that non-numerical args end up with
; a clean zero.
ld ix, 0
ld de, argspecTbl
; DE now points the the "argspec char" part of the entry, but what
; we're comparing in the loop is the string next to it. Let's offset
@ -181,7 +176,7 @@ parseArg:
; (HL) has no parens
call .maybeParseExpr
jr nz, .nomatch
; We have a proper number in no parens. Number in IX.
; We have a proper number in no parens. Number in DE.
ld a, 'N'
jr .end
.withParens:
@ -208,23 +203,20 @@ parseArg:
.parseNumberInParens:
call .maybeParseExpr
jr nz, .nomatch
; We have a proper number in parens. Number in IX
; is '-' in B? if yes, we need to negate the low part of IX
; We have a proper number in parens. Number in DE
; is '-' in B? if yes, we need to negate the low part of DE
ld a, b
cp '-'
jr nz, .dontNegateIX
; we need to negate the low part of IX
jr nz, .dontNegateDE
; we need to negate the low part of DE
; TODO: when parsing routines properly support unary negative numbers,
; We could replace this complicated scheme below with a nice hack where
; we start parsing our displacement number at the '+' and '-' char.
; HL isn't needed anymore and can be destroyed.
push ix \ pop hl
ld a, l
ld a, e
neg
ld l, a
push hl \ pop ix
.dontNegateIX:
ld e, a
.dontNegateDE:
ld a, c ; M, x, or y
jr .end
.nomatch:
@ -235,9 +227,13 @@ parseArg:
; found the matching argspec row. Our result is one byte left of DE.
dec de
ld a, (de)
; When we have non-numerical args, we set DE to zero to have a clean
; result.
ld de, 0
.end:
pop hl
pop de
pop bc
ret
@ -247,6 +243,7 @@ parseArg:
; harmless, but in some cases it causes false failures. For example,
; a "-" operator can cause is to falsely overflow and generate
; truncation error.
ld de, 0 ; in first pass, return a clean zero
call zasmIsFirstPass
ret z
jp parseExpr
@ -817,27 +814,18 @@ spitUpcode:
ld (INS_UPCODE+2), a
ret
; Parse argument in (HL) and place it in (DE)
; DE is not preserved
; Parse argument in (HL) and place it in (IX)
; Sets Z on success, reset on error.
processArg:
call parseArg
cp 0xff
jr z, .error
ld (de), a
; When A is a number, IX is set with the value of that number. Because
ld (ix), a
; When A is a number, DE is set with the value of that number. Because
; We don't use the space allocated to store those numbers in any other
; occasion, we store IX there unconditonally, LSB first.
inc de
ex (sp), ix ; (SP) is kept in IX and will be restored
ex (sp), hl ; old HL is on (SP)
ld a, l
ld (de), a
inc de
ld a, h
ld (de), a
ex (sp), hl ; restore old HL from (SP)
ex (sp), ix ; restore old (SP) from IX
; occasion, we store DE there unconditonally, LSB first.
ld (ix+1), e
ld (ix+2), d
cp a ; ensure Z
ret
.error:
@ -860,14 +848,14 @@ parseInstruction:
ld (INS_CURARG2), a
call readWord
jr nz, .nomorearg
ld de, INS_CURARG1
ld ix, INS_CURARG1
call processArg
jr nz, .end ; A is set to error, Z is unset
call readComma
jr nz, .nomorearg
call readWord
jr nz, .badfmt
ld de, INS_CURARG2
ld ix, INS_CURARG2
call processArg
jr nz, .end ; A is set to error, Z is unset
.nomorearg:

View File

@ -6,9 +6,15 @@
* until it halts. The return code is the value of the register A at halt time.
*/
static void iowr_stderr(uint8_t val)
{
fputc(val, stderr);
}
int main()
{
Machine *m = emul_init();
m->iowr[0] = iowr_stderr;
// read stdin in mem
int i = 0;
int c = getchar();
@ -22,6 +28,7 @@ int main()
return 1;
}
emul_loop();
if (m->cpu.R1.wr.HL)
return m->cpu.R1.br.A;
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,62 @@
; *** requirements ***
; ascii.h
; core
; stdio
; lib/ari
; lib/fmt
testNum: .db 1
STDIO_PUTC:
out (0), a
cp a
ret
STDIO_GETC:
jp unsetZ
assertZ:
ret z
ld hl, .msg
call printstr
jp fail
.msg:
.db "Z not set", CR, LF, 0
assertNZ:
ret nz
ld hl, .msg
call printstr
jp fail
.msg:
.db "Z set", CR, LF, 0
; Assert that HL == DE
assertEQW:
ld a, h
cp d
jr nz, .fail
ld a, l
cp e
ret z
.fail:
call printHexPair
call printcrlf
ex de, hl
call printHexPair
call printcrlf
ld hl, .msg
call printstr
jp fail
.msg:
.db "HL != DE", CR, LF, 0
nexttest:
ld a, (testNum)
inc a
ld (testNum), a
ret
fail:
ld a, (testNum)
halt

View File

@ -11,7 +11,7 @@ APPS="${BASE}/apps"
chk() {
echo "Running test $1"
if ! ${ZASM} "${KERNEL}" "${APPS}" < $1 | ${RUNBIN}; then
if ! ${ZASM} "${KERNEL}" "${APPS}" common.asm < $1 | ${RUNBIN}; then
echo "failed with code ${PIPESTATUS[1]}"
exit 1
fi
@ -22,7 +22,7 @@ if [ ! -z $1 ]; then
exit 0
fi
for fn in *.asm; do
for fn in test_*.asm; do
chk "${fn}"
done

View File

@ -1,5 +1,6 @@
jp test
.inc "ascii.h"
.inc "core.asm"
.inc "str.asm"
.inc "lib/util.asm"
@ -8,6 +9,9 @@ jp test
.equ EXPR_PARSE parseLiteral
.inc "lib/expr.asm"
.inc "basic/parse.asm"
.inc "lib/fmt.asm"
.inc "stdio.asm"
.inc "common.asm"
test:
ld sp, 0xffff
@ -55,21 +59,21 @@ testParseThruth:
.true:
call parseTruth
jp nz, fail
call assertZ
or a
jp z, fail
call assertNZ
jp nexttest
.false:
call parseTruth
jp nz, fail
call assertZ
or a
jp nz, fail
call assertZ
jp nexttest
.error:
call parseTruth
jp z, fail
call assertNZ
jp nexttest
.t1: .db "42", 0
@ -88,17 +92,4 @@ testParseThruth:
.f6: .db "2<=1", 0
.e1: .db "foo", 0
testNum: .db 1
nexttest:
ld a, (testNum)
inc a
ld (testNum), a
ret
fail:
ld a, (testNum)
halt
; used as RAM
sandbox:
STDIO_RAMSTART:

View File

@ -5,8 +5,14 @@
jp test
.inc "ascii.h"
.inc "core.asm"
.inc "lib/ari.asm"
.inc "lib/fmt.asm"
.inc "stdio.asm"
.inc "common.asm"
dummyLabel:
testNum: .db 1
.equ dummyLabel 0x42
@ -37,37 +43,25 @@ test:
call nexttest
; Test that .equ can override label
ld a, 0x42
ld de, 0x42
ld hl, dummyLabel
cp l
jp nz, fail
call assertEQW
call nexttest
; test that "@" is updated by a .org directive
ld hl, AFTER_ORG
ld de, 0x1234
or a ; clear carry
sbc hl, de
jp nz, fail
call assertEQW
call nexttest
; test that AND affects the Z flag
ld a, 0x69
and 0x80
jp nz, fail
call assertZ
call nexttest
; success
xor a
halt
nexttest:
ld a, (testNum)
inc a
ld (testNum), a
ret
fail:
ld a, (testNum)
halt
STDIO_RAMSTART:

View File

@ -9,10 +9,12 @@
jp test
.inc "ascii.h"
.inc "core.asm"
.inc "str.asm"
.inc "lib/util.asm"
.inc "lib/ari.asm"
.inc "lib/fmt.asm"
.inc "zasm/util.asm"
.inc "zasm/const.asm"
.inc "lib/parse.asm"
@ -21,6 +23,9 @@ jp test
.inc "zasm/symbol.asm"
.equ EXPR_PARSE parseNumberOrSymbol
.inc "lib/expr.asm"
.equ STDIO_RAMSTART SYM_RAMEND
.inc "stdio.asm"
.inc "common.asm"
; Pretend that we aren't in first pass
zasmIsFirstPass:
@ -29,8 +34,6 @@ zasmIsFirstPass:
zasmGetPC:
ret
testNum: .db 1
s1: .db "2+2", 0
s2: .db "0x4001+0x22", 0
s3: .db "FOO+BAR", 0
@ -44,29 +47,22 @@ sBAR: .db "BAR", 0
test:
ld sp, 0xffff
; New-style tests
call testParseExpr
; Old-style tests, not touching them now.
ld hl, s1
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
or a
jp nz, fail
ld a, l
cp 4
jp nz, fail
call assertZ
ld hl, 4
call assertEQW
call nexttest
ld hl, s2
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
cp 0x40
jp nz, fail
ld a, l
cp 0x23
jp nz, fail
call assertZ
ld hl, 0x4023
call assertEQW
call nexttest
; before the next test, let's set up FOO and BAR symbols
@ -82,54 +78,32 @@ test:
ld hl, s3
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
cp 0x40
jp nz, fail
ld a, l
cp 0x20
jp nz, fail
call assertZ
ld hl, 0x4020
call assertEQW
call nexttest
ld hl, s4
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
or a
jp nz, fail
ld a, l
cp 0x60
jp nz, fail
call assertZ
ld hl, 0x60
call assertEQW
call nexttest
ld hl, s5
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
cp 0x3f
jp nz, fail
ld a, l
cp 0xfd
jp nz, fail
call assertZ
ld hl, 0x3ffd
call assertEQW
call nexttest
ld hl, s6
call parseExpr
jp nz, fail
push ix \ pop hl
ld a, h
cp 0x40
jp nz, fail
ld a, l
cp 0x80
jp nz, fail
call assertZ
ld hl, 0x4080
call assertEQW
call nexttest
; New-style tests
call testParseExpr
; success
xor a
halt
@ -151,20 +125,18 @@ testParseExpr:
call .testEQ
ld iy, .t8
call .testEQ
ld iy, .t9
call .testEQ
ret
.testEQ:
push iy \ pop hl
inc hl \ inc hl
call parseExpr
jp nz, fail
push ix \ pop de
ld a, e
cp (iy)
jp nz, fail
ld a, d
cp (iy+1)
jp nz, fail
call assertZ
ld l, (iy)
ld h, (iy+1)
call assertEQW
jp nexttest
.t1:
@ -191,13 +163,6 @@ testParseExpr:
.t8:
.dw 0xffff
.db "-1", 0
nexttest:
ld a, (testNum)
inc a
ld (testNum), a
ret
fail:
ld a, (testNum)
halt
.t9:
.dw 10
.db "2*3+4", 0