2019-05-10 20:32:05 -04:00
|
|
|
; zasm
|
|
|
|
;
|
|
|
|
; Reads input from specified blkdev ID, assemble the binary in two passes and
|
|
|
|
; spit the result in another specified blkdev ID.
|
|
|
|
;
|
|
|
|
; We don't buffer the whole source in memory, so we need our input blkdev to
|
|
|
|
; support Seek so we can read the file a second time. So, for input, we need
|
|
|
|
; GetC and Seek.
|
|
|
|
;
|
|
|
|
; For output, we only need PutC. Output doesn't start until the second pass.
|
|
|
|
;
|
|
|
|
; The goal of the second pass is to assign values to all symbols so that we
|
|
|
|
; can have forward references (instructions referencing a label that happens
|
|
|
|
; later).
|
|
|
|
;
|
|
|
|
; Labels and constants are both treated the same way, that is, they can be
|
|
|
|
; forward-referenced in instructions. ".equ" directives, however, are evaluated
|
|
|
|
; during the first pass so forward references are not allowed.
|
|
|
|
;
|
2019-05-09 14:09:40 -04:00
|
|
|
; *** Requirements ***
|
2019-05-09 15:36:03 -04:00
|
|
|
; blockdev
|
2019-05-17 09:50:11 -04:00
|
|
|
; strncmp
|
|
|
|
; addDE
|
|
|
|
; addHL
|
|
|
|
; upcase
|
|
|
|
; unsetZ
|
|
|
|
; intoDE
|
|
|
|
; intoHL
|
2019-05-19 09:06:24 -04:00
|
|
|
; writeHLinDE
|
2019-05-17 09:50:11 -04:00
|
|
|
; findchar
|
2019-05-17 23:00:57 -04:00
|
|
|
; parseHex
|
|
|
|
; parseHexPair
|
2019-05-17 09:50:11 -04:00
|
|
|
; blkSel
|
|
|
|
; fsFindFN
|
|
|
|
; fsOpen
|
|
|
|
; fsGetC
|
|
|
|
; fsSeek
|
|
|
|
; fsTell
|
2019-05-19 09:14:40 -04:00
|
|
|
; ZASM_RAMSTART (where we put our variables in RAM)
|
2019-05-16 21:15:00 -04:00
|
|
|
; FS_HANDLE_SIZE
|
2019-04-30 15:51:39 -04:00
|
|
|
|
2019-05-10 20:32:05 -04:00
|
|
|
; *** Variables ***
|
|
|
|
|
|
|
|
; A bool flag indicating that we're on first pass. When we are, we don't care
|
|
|
|
; about actual output, but only about the length of each upcode. This means
|
|
|
|
; that when we parse instructions and directive that error out because of a
|
|
|
|
; missing symbol, we don't error out and just write down a dummy value.
|
2019-05-19 09:14:40 -04:00
|
|
|
.equ ZASM_FIRST_PASS ZASM_RAMSTART
|
2019-05-15 20:07:21 -04:00
|
|
|
; whether we're in "local pass", that is, in local label scanning mode. During
|
|
|
|
; this special pass, ZASM_FIRST_PASS will also be set so that the rest of the
|
|
|
|
; code behaves as is we were in the first pass.
|
2019-05-17 14:58:16 -04:00
|
|
|
.equ ZASM_LOCAL_PASS ZASM_FIRST_PASS+1
|
|
|
|
; What IO_PC was when we started our context
|
2019-05-16 21:15:00 -04:00
|
|
|
.equ ZASM_CTX_PC ZASM_LOCAL_PASS+1
|
2019-05-15 20:07:21 -04:00
|
|
|
.equ ZASM_RAMEND ZASM_CTX_PC+2
|
2019-05-10 20:32:05 -04:00
|
|
|
|
2019-05-17 20:22:32 -04:00
|
|
|
#include "zasm/util.asm"
|
2019-05-10 20:32:05 -04:00
|
|
|
.equ IO_RAMSTART ZASM_RAMEND
|
2019-05-17 20:22:32 -04:00
|
|
|
#include "zasm/io.asm"
|
2019-05-18 20:31:52 -04:00
|
|
|
.equ TOK_RAMSTART IO_RAMEND
|
2019-05-17 20:22:32 -04:00
|
|
|
#include "zasm/tok.asm"
|
|
|
|
#include "zasm/parse.asm"
|
|
|
|
#include "zasm/expr.asm"
|
|
|
|
#include "zasm/instr.asm"
|
2019-05-18 20:31:52 -04:00
|
|
|
.equ DIREC_RAMSTART TOK_RAMEND
|
2019-05-17 20:22:32 -04:00
|
|
|
#include "zasm/directive.asm"
|
2019-05-11 22:11:05 -04:00
|
|
|
.equ SYM_RAMSTART DIREC_RAMEND
|
2019-05-17 20:22:32 -04:00
|
|
|
#include "zasm/symbol.asm"
|
2019-05-10 18:20:43 -04:00
|
|
|
|
|
|
|
; Read file through blockdev ID in H and outputs its upcodes through blockdev
|
|
|
|
; ID in L.
|
2019-05-10 20:32:05 -04:00
|
|
|
zasmMain:
|
2019-05-13 20:23:10 -04:00
|
|
|
; Init I/O
|
2019-05-10 18:20:43 -04:00
|
|
|
ld a, h
|
|
|
|
ld de, IO_IN_GETC
|
2019-05-17 09:50:11 -04:00
|
|
|
call blkSel
|
2019-05-10 18:20:43 -04:00
|
|
|
ld a, l
|
|
|
|
ld de, IO_OUT_GETC
|
2019-05-17 09:50:11 -04:00
|
|
|
call blkSel
|
2019-05-15 20:07:21 -04:00
|
|
|
|
2019-05-13 20:23:10 -04:00
|
|
|
; Init modules
|
2019-05-15 20:07:21 -04:00
|
|
|
xor a
|
|
|
|
ld (ZASM_LOCAL_PASS), a
|
2019-05-16 07:53:42 -04:00
|
|
|
call ioInit
|
2019-05-13 20:23:10 -04:00
|
|
|
call symInit
|
|
|
|
|
2019-05-10 20:32:05 -04:00
|
|
|
; First pass
|
|
|
|
ld a, 1
|
|
|
|
ld (ZASM_FIRST_PASS), a
|
|
|
|
call zasmParseFile
|
2019-05-12 21:31:11 -04:00
|
|
|
ret nz
|
2019-05-10 20:32:05 -04:00
|
|
|
; Second pass
|
2019-05-16 21:15:00 -04:00
|
|
|
call ioRewind
|
2019-05-10 20:32:05 -04:00
|
|
|
xor a
|
|
|
|
ld (ZASM_FIRST_PASS), a
|
|
|
|
call zasmParseFile
|
|
|
|
ret
|
|
|
|
|
|
|
|
; Sets Z according to whether we're in first pass.
|
|
|
|
zasmIsFirstPass:
|
|
|
|
ld a, (ZASM_FIRST_PASS)
|
|
|
|
cp 1
|
2019-04-30 15:51:39 -04:00
|
|
|
ret
|
|
|
|
|
2019-05-15 20:07:21 -04:00
|
|
|
; Sets Z according to whether we're in local pass.
|
|
|
|
zasmIsLocalPass:
|
|
|
|
ld a, (ZASM_LOCAL_PASS)
|
|
|
|
cp 1
|
|
|
|
ret
|
|
|
|
|
2019-05-12 21:44:59 -04:00
|
|
|
; Repeatedly reads lines from IO, assemble them and spit the binary code in
|
|
|
|
; IO. Z is set on success, unset on error. DE contains the last line number to
|
|
|
|
; be read (first line is 1).
|
2019-05-10 20:32:05 -04:00
|
|
|
zasmParseFile:
|
2019-05-17 14:58:16 -04:00
|
|
|
call ioResetPC
|
2019-05-10 20:32:05 -04:00
|
|
|
.loop:
|
|
|
|
call parseLine
|
2019-05-12 21:31:11 -04:00
|
|
|
ret nz ; error
|
2019-05-16 07:53:42 -04:00
|
|
|
ld a, b ; TOK_*
|
|
|
|
cp TOK_EOF
|
2019-05-15 20:07:21 -04:00
|
|
|
jr z, .eof
|
|
|
|
jr .loop
|
|
|
|
.eof:
|
|
|
|
call zasmIsLocalPass
|
|
|
|
jr nz, .end ; EOF and not local pass
|
|
|
|
; we're in local pass and EOF. Unwind this
|
|
|
|
call _endLocalPass
|
2019-05-10 20:32:05 -04:00
|
|
|
jr .loop
|
2019-05-15 20:07:21 -04:00
|
|
|
.end:
|
|
|
|
cp a ; ensure Z
|
|
|
|
ret
|
2019-05-10 20:32:05 -04:00
|
|
|
|
2019-05-16 07:53:42 -04:00
|
|
|
; Parse next token and accompanying args (when relevant) in I/O, write the
|
2019-05-17 14:58:16 -04:00
|
|
|
; resulting opcode(s) through ioPutC and increases (IO_PC) by the number of
|
2019-05-16 07:53:42 -04:00
|
|
|
; bytes written. BC is set to the result of the call to tokenize.
|
|
|
|
; Sets Z if parse was successful, unset if there was an error. EOF is not an
|
|
|
|
; error.
|
2019-04-30 15:51:39 -04:00
|
|
|
parseLine:
|
|
|
|
call tokenize
|
2019-04-30 22:27:11 -04:00
|
|
|
ld a, b ; TOK_*
|
2019-04-30 21:40:22 -04:00
|
|
|
cp TOK_INSTR
|
2019-05-15 15:16:35 -04:00
|
|
|
jp z, _parseInstr
|
2019-05-01 11:26:41 -04:00
|
|
|
cp TOK_DIRECTIVE
|
2019-05-15 15:16:35 -04:00
|
|
|
jp z, _parseDirec
|
2019-05-09 21:21:08 -04:00
|
|
|
cp TOK_LABEL
|
2019-05-16 07:53:42 -04:00
|
|
|
jr z, _parseLabel
|
|
|
|
cp TOK_EOF
|
|
|
|
ret ; Z is correct. If EOF, Z is set and not an
|
2019-05-14 14:32:12 -04:00
|
|
|
; error, otherwise, it means bad token and
|
|
|
|
; errors out.
|
|
|
|
|
|
|
|
_parseInstr:
|
2019-04-30 22:27:11 -04:00
|
|
|
ld a, c ; I_*
|
2019-05-17 15:35:49 -04:00
|
|
|
jp parseInstruction
|
2019-05-14 14:32:12 -04:00
|
|
|
|
|
|
|
_parseDirec:
|
2019-05-01 11:26:41 -04:00
|
|
|
ld a, c ; D_*
|
|
|
|
call parseDirective
|
2019-05-17 15:14:38 -04:00
|
|
|
cp a ; ensure Z
|
2019-05-14 14:32:12 -04:00
|
|
|
ret
|
|
|
|
|
|
|
|
_parseLabel:
|
2019-05-09 21:21:08 -04:00
|
|
|
; The string in (scratchpad) is a label with its trailing ':' removed.
|
|
|
|
ld hl, scratchpad
|
2019-05-15 20:07:21 -04:00
|
|
|
|
|
|
|
call zasmIsLocalPass
|
|
|
|
jr z, .processLocalPass
|
|
|
|
|
|
|
|
; Is this a local label? If yes, we don't process it in the context of
|
|
|
|
; parseLine, whether it's first or second pass. Local labels are only
|
|
|
|
; parsed during the Local Pass
|
|
|
|
call symIsLabelLocal
|
|
|
|
jr z, .success ; local? don't do anything.
|
|
|
|
|
2019-05-13 20:23:10 -04:00
|
|
|
call zasmIsFirstPass
|
|
|
|
jr z, .registerLabel ; When we encounter a label in the first
|
|
|
|
; pass, we register it in the symbol
|
|
|
|
; list
|
2019-05-15 20:07:21 -04:00
|
|
|
; At this point, we're in second pass, we've encountered a global label
|
|
|
|
; and we'll soon continue processing our file. However, before we do
|
|
|
|
; that, we should process our local labels.
|
|
|
|
call _beginLocalPass
|
|
|
|
jr .success
|
|
|
|
.processLocalPass:
|
2019-05-13 20:23:10 -04:00
|
|
|
call symIsLabelLocal
|
2019-05-15 20:07:21 -04:00
|
|
|
jr z, .registerLabel ; local label? all good, register it
|
|
|
|
; normally
|
|
|
|
; not a local label? Then we need to end local pass
|
|
|
|
call _endLocalPass
|
|
|
|
jr .success
|
2019-05-13 20:23:10 -04:00
|
|
|
.registerLabel:
|
2019-05-17 14:58:16 -04:00
|
|
|
ld de, (IO_PC)
|
2019-05-09 21:21:08 -04:00
|
|
|
call symRegister
|
2019-05-13 20:23:10 -04:00
|
|
|
jr nz, .error
|
|
|
|
; continue to .success
|
2019-05-01 10:16:57 -04:00
|
|
|
.success:
|
|
|
|
xor a ; ensure Z
|
2019-05-14 14:32:12 -04:00
|
|
|
ret
|
2019-04-30 15:51:39 -04:00
|
|
|
.error:
|
2019-05-17 09:50:11 -04:00
|
|
|
call unsetZ
|
2019-04-30 15:51:39 -04:00
|
|
|
ret
|
2019-05-15 20:07:21 -04:00
|
|
|
|
|
|
|
_beginLocalPass:
|
|
|
|
; remember were I/O was
|
2019-05-16 21:15:00 -04:00
|
|
|
call ioSavePos
|
2019-05-15 20:07:21 -04:00
|
|
|
; Remember where PC was
|
2019-05-17 14:58:16 -04:00
|
|
|
ld hl, (IO_PC)
|
2019-05-15 20:07:21 -04:00
|
|
|
ld (ZASM_CTX_PC), hl
|
|
|
|
; Fake first pass
|
|
|
|
ld a, 1
|
|
|
|
ld (ZASM_FIRST_PASS), a
|
|
|
|
; Set local pass
|
|
|
|
ld (ZASM_LOCAL_PASS), a
|
|
|
|
; Empty local label registry
|
|
|
|
xor a
|
|
|
|
ld (SYM_LOC_NAMES), a
|
|
|
|
call symSelectLocalRegistry
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
_endLocalPass:
|
|
|
|
call symSelectGlobalRegistry
|
|
|
|
; recall I/O pos
|
2019-05-16 21:15:00 -04:00
|
|
|
call ioRecallPos
|
2019-05-15 20:07:21 -04:00
|
|
|
; recall PC
|
|
|
|
ld hl, (ZASM_CTX_PC)
|
2019-05-17 14:58:16 -04:00
|
|
|
ld (IO_PC), hl
|
2019-05-15 20:07:21 -04:00
|
|
|
; unfake first pass
|
|
|
|
xor a
|
|
|
|
ld (ZASM_FIRST_PASS), a
|
|
|
|
; Unset local pass
|
|
|
|
ld (ZASM_LOCAL_PASS), a
|
|
|
|
cp a ; ensure Z
|
|
|
|
ret
|