-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.fs
69 lines (53 loc) · 1.56 KB
/
main.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
\ Main routines for loading and running the Z-machine
VARIABLE breakpoint
: break ( ra -- ) breakpoint ! ;
\ Main interpreter loop.
: interp ( -- )
BEGIN execute-op AGAIN
;
VARIABLE story-file
\ Loads the named file into the machine.
: load-file ( c-addr u -- )
R/O BIN OPEN-FILE ABORT" Could not open story file" ( fileid )
story-file !
;
\ Loads the story file from disk into memory.
\ Illegal to call when the story-file is not a valid, open fileid.
: reload-file ( -- )
ram ( c-addr )
story-file @ FILE-SIZE ABORT" Failed to read file size" ( c-addr dsize )
d>s ( c-addr size ) \ We can assume the files are not > 4GB!
0 0 story-file @ ( c-addr size 0 0 fileid )
REPOSITION-FILE ABORT" Failed to reposition to 0" ( c-addr size )
story-file @ READ-FILE ( u ior )
ABORT" Failed to read the story file into memory." ( bytes-read )
." Read " . ." bytes of story file." cr ( )
;
\ Sets the deferred word pa to the appropriate calculation, based on version.
: init-packed-addrs ( -- )
version
dup 8 = IF drop ['] pa-late IS pa EXIT THEN
dup 6 >= IF ABORT" Versions 6 and 7 are not supported. Use v3-5 or v8." THEN
4 >= IF ['] pa-mid IS pa EXIT THEN
['] pa-early IS pa
;
\ Runs the restart process and sets the Z-machine running.
\ restart
:noname ( -- )
\ Reset some internal state.
true-seed
stack-top sp !
reload-file
init-packed-addrs
init-header-bits
hdr-init-pc w@ pc !
; IS restart
\ Main routine
\ parse-file-name
\ restart
\ For testing
\ S" Zork1.z3"
S" Zork1.z5"
\ S" etude.z5"
\ S" czech.z5"
load-file restart interp