Skip to content

Commit 4393330

Browse files
Save and load #1 (#9)
1 parent 63ca0e6 commit 4393330

File tree

3 files changed

+159
-13
lines changed

3 files changed

+159
-13
lines changed

src/basic.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
; p6.1 alter 2.22p5 5.3 to make compatible with cc65 linking.
4848
; p6.2 removed use of page 2; moved cc* to zp, moved Ibuff to cc65 memory.
4949

50-
.exportzp LAB_WARM, IrqBase, NmiBase
51-
.export LAB_COLD, LAB_FCER
50+
.exportzp LAB_WARM, IrqBase, NmiBase, Dtypef
51+
.export LAB_COLD, LAB_FCER, LAB_14BD, LAB_EVEX, LAB_SNER, LAB_22B6, LAB_1463, LAB_RMSG
5252

5353
.import V_INPT, V_OUTP, V_LOAD, V_SAVE, V_USR
5454
.import __HEADER_START__, __RAM_START__, __RAM_SIZE__, __STACKSIZE__, __IBUFFSIZE__

src/min_mon.s

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,166 @@
44
.include "rp6502.inc"
55

66
.export V_INPT, V_OUTP, V_LOAD, V_SAVE
7+
.import LAB_14BD, LAB_EVEX, LAB_SNER, LAB_22B6, LAB_1463, LAB_RMSG
8+
.importzp Dtypef, ptr1, tmp1
9+
10+
.data
11+
12+
fd_in:
13+
.byte $FF ; input file descriptor, or negative for ACIA
14+
fd_out:
15+
.byte $FF ; output file descriptor, or negative for ACIA
16+
17+
.code
718

8-
V_OUTP: ; byte out to simulated ACIA
9-
BIT RIA_READY
10-
BPL V_OUTP ; wait for FIFO
11-
STA RIA_TX ; save byte to simulated ACIA
12-
RTS
1319
V_INPT: ; byte in from simulated ACIA
20+
BIT fd_in ; check for read fd
21+
BPL read ; use read() handler
1422
BIT RIA_READY
1523
BVC LAB_nobyw ; branch if no byte waiting
1624
LDA RIA_RX ; get byte from simulated ACIA
1725
SEC ; flag byte received
1826
RTS
1927
LAB_nobyw:
2028
CLC ; flag no byte received
29+
RTS
30+
read:
31+
LDA tmp1
32+
BEQ read_read
33+
DEC tmp1
34+
LDA #$0D
35+
SEC
36+
RTS
37+
read_read:
38+
LDA #$01
39+
STA RIA_XSTACK
40+
LDA fd_in
41+
STA RIA_A
42+
LDA #RIA_OP_READ_XSTACK
43+
STA RIA_OP ; int read_xstack(void *buf, unsigned count, int fildes)
44+
read_busy:
45+
BIT RIA_BUSY
46+
BMI read_busy
47+
LDA RIA_XSTACK
48+
CMP #$0A ; LF and CRLF endings supported
49+
BEQ read_cr
50+
CMP #$00 ; empty stack always returns 0
51+
BNE read_done
52+
LDA fd_in ; eof
53+
STA RIA_A
54+
LDA #RIA_OP_CLOSE
55+
STA RIA_OP ; int close(int fildes)
56+
read_close:
57+
BIT RIA_BUSY
58+
BMI read_close
59+
LDA #$FF
60+
STA fd_in ; restore V_INPT to ACIA
61+
STA fd_out ; restore V_OUTP to ACIA
62+
PHY ; print "Ready"
63+
LDA #<(LAB_RMSG)
64+
STA ptr1
65+
LDA #>(LAB_RMSG)
66+
STA ptr1+1
67+
LDY #$00
68+
read_ready_loop:
69+
LDA (ptr1),y
70+
JSR V_OUTP_wait
71+
INY
72+
CPY #$07
73+
BMI read_ready_loop
74+
PLY
75+
read_cr:
76+
LDA #$0D
77+
read_done:
78+
SEC
79+
RTS
80+
81+
82+
V_OUTP: ; byte out to simulated ACIA
83+
BIT fd_out ; check for write fd
84+
BPL write ; use write() handler
85+
V_OUTP_wait:
86+
BIT RIA_READY ; check ready bit
87+
BPL V_OUTP_wait ; wait for FIFO
88+
STA RIA_TX ; save byte to simulated ACIA
89+
RTS
90+
write:
91+
BIT fd_in ; check for read fd
92+
BPL write_skip ; dump load echos to null
93+
CMP #$0D ; ASCII 13 CR
94+
BEQ write_skip ; filter CR, saves are LF only
95+
STA RIA_XSTACK
96+
LDA fd_out
97+
STA RIA_A
98+
LDA #RIA_OP_WRITE_XSTACK
99+
STA RIA_OP ; int write_xstack(const void *buf, unsigned count, int fildes)
100+
write_busy:
101+
BIT RIA_BUSY
102+
BMI write_busy ; TODO check for errors
103+
write_skip:
104+
RTS
105+
106+
21107
V_LOAD: ; empty load vector for EhBASIC
22-
V_SAVE: ; empty save vector for EhBASIC
108+
LDA #$01 ; O_RDONLY
109+
JSR open
110+
BMI syntax_error ; TODO file error instead of syntax
111+
STA fd_in ; redirect V_INPT from fd
112+
STA fd_out ; redirect V_OUTP to null
113+
TSX ; LAB_NEW clobbers stack, save the return
114+
INX
115+
LDA $100,X
116+
STA ptr1
117+
INX
118+
LDA $100,X
119+
STA ptr1+1
120+
JSR LAB_1463 ; LAB_NEW
121+
LDA ptr1+1 ; restore the return address clobbered by new
122+
PHA
123+
LDA ptr1
124+
PHA
125+
LDA #$01 ; preamble newlines
126+
STA tmp1
127+
RTS
128+
129+
130+
V_SAVE:
131+
LDA #$32 ; O_TRUNC | O_CREAT | O_WRONLY
132+
JSR open
133+
BMI syntax_error ; TODO file error instead of syntax
134+
STA fd_out ; redirect V_OUTP to fd
135+
JSR LAB_14BD ; LAB_LIST
136+
LDA fd_out
137+
STA RIA_A ; to be used by close()
138+
LDA #$FF
139+
STA fd_out ; restore V_OUTP to ACIA
140+
LDA #RIA_OP_CLOSE
141+
STA RIA_OP ; int close(int fildes)
142+
JSR RIA_SPIN
143+
BMI syntax_error ; TODO file error instead of syntax
23144
RTS
145+
146+
147+
open:
148+
STA RIA_A ; file open options
149+
JSR LAB_EVEX ; evaluate expression
150+
LDA Dtypef ; data type flag, $FF=string, $00=numeric
151+
BEQ syntax_error ; syntax error if not string
152+
JSR LAB_22B6 ; obtain string
153+
STX ptr1 ; pointer low byte
154+
STY ptr1+1 ; pointer high byte
155+
TAY ; length
156+
BEQ syntax_error ; syntax error if empty string
157+
push_filename:
158+
DEY
159+
LDA (ptr1), Y
160+
STA RIA_XSTACK
161+
TYA
162+
BNE push_filename
163+
LDA #RIA_OP_OPEN
164+
STA RIA_OP ; int open(const char *path, int oflag)
165+
JMP RIA_SPIN
166+
167+
168+
syntax_error:
169+
JMP LAB_SNER ; far jump

tools/rp6502.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ def add_rp6502_file(self, file):
178178
# Decode first line as cp850 because binary garbage can
179179
# raise here before our better message gets to the user.
180180
command = f.readline().decode("cp850")
181-
if not re.match("^#![Rr][Pp]6502\n$", command):
181+
if not re.match("^#![Rr][Pp]6502(\r|)\n$", command):
182182
raise RuntimeError(f"Invalid RP6502 ROM file: {file}")
183183
while True:
184-
command = f.readline().decode("ascii")
184+
command = f.readline().decode("ascii").rstrip()
185185
if len(command) == 0:
186186
break
187187
se = re.search("^ *(# )", command)
188188
if se:
189-
self.add_help(command[se.start(1) + 2 :].rstrip())
189+
self.add_help(command[se.start(1) + 2 :])
190190
continue
191191
if re.search("^ *#$", command):
192192
self.add_help("")
@@ -197,7 +197,7 @@ def add_rp6502_file(self, file):
197197
def str_to_address(str):
198198
"""Supports $FFFF number format."""
199199
if str:
200-
str = re.sub("^\$", "0x", str)
200+
str = re.sub("^\\$", "0x", str)
201201
if re.match("^(0x|)[0-9A-Fa-f]*$", str):
202202
return eval(str)
203203
else:
@@ -317,7 +317,7 @@ def exec_args():
317317
def str_to_address(parser, str, errmsg):
318318
"""Supports $FFFF number format."""
319319
if str:
320-
str = re.sub("^\$", "0x", str)
320+
str = re.sub("^\\$", "0x", str)
321321
if re.match("^(0x|)[0-9A-Fa-f]*$", str):
322322
return eval(str)
323323
else:

0 commit comments

Comments
 (0)