-
Notifications
You must be signed in to change notification settings - Fork 2
/
riscsd.pas
264 lines (196 loc) · 6.57 KB
/
riscsd.pas
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
(*********************************
Reading and writing to and from
the Oberon filesystem, which is kept
in one single file. (MG)
*********************************)
UNIT riscsd;
INTERFACE
USES sysutils, riscglob;
CONST BUFSBYTE = 512;
BUFSWORD = BUFSBYTE DIV 4;
TYPE
diskstatetype = (diskCommand, diskRead, diskWrite, diskWriting);
bufty = ARRAY[0..Pred(BUFSWORD) ] OF uint32_t;
buftyp2 = ARRAY[0..Pred(BUFSWORD)+2] OF uint32_t;
bytebufty = ARRAY[0..pred(BUFSBYTE) ] OF uint8_t;
Diskty = OBJECT
PRIVATE
state : diskstatetype;
myfile: THandle;
rx_buf: bufty;
rx_idx: longint;
tx_buf: buftyp2;
tx_cnt: longint;
tx_idx: longint;
offset : uint32_t;
PUBLIC
sdcard : Boolean;
CONSTRUCTOR init(filename : string);
PROCEDURE run_command;
PRIVATE
PROCEDURE read_sector(VAR buffer : bufty);
PROCEDURE read_sector2(VAR buffer : buftyp2);
PROCEDURE write_sector(buffer : bufty);
PUBLIC
FUNCTION read_: uint32_t;
PROCEDURE write_(value: uint32_t);
DESTRUCTOR done;
END;
VAR disk : Diskty;
IMPLEMENTATION
CONSTRUCTOR diskty.init(filename : string);
VAR buffer : bufty;
BEGIN
state := diskCommand;
sdcard := False;
buffer[0] := 0;
{$I-}
writeln(' Filename : ', filename);
myfile := fileOpen(filename, fmOpenReadWrite);
{$I+}
IF myfile = 0 THEN
BEGIN
writeln('Can''t open file : ', filename,' ', ioresult);
exit;
END;
(* Check FOR filesystem-only image, starting directly at sector 1 (DiskAdr 29) *)
read_sector(buffer);
IF (buffer[0] = $9B1EA38D) THEN offset := $80002 ELSE offset := 0;
writeln(' File Offset : ', offset);
sdcard := True;
END;
DESTRUCTOR diskty.done;
BEGIN
fileclose(myfile);
END;
PROCEDURE diskty.write_(value: uint32_t);
BEGIN
inc(tx_idx);
(* case statements in Pascal are breaking the case loop if
the first condition is true, not so in C *)
CASE state of
diskCommand: BEGIN
IF ((byte(value)<>$FF) OR (rx_idx<>0)) THEN
BEGIN
rx_buf[rx_idx]:= value;
inc(rx_idx);
IF rx_idx = 6 THEN
BEGIN
run_command;
rx_idx:= 0;
END;
END;
END;
diskRead: BEGIN
IF tx_idx = tx_cnt THEN
BEGIN
state:= diskCommand;
tx_cnt:= 0;
tx_idx:= 0;
END;
END;
diskWrite: BEGIN
IF value = 254 THEN state:= diskWriting;
END;
diskWriting: BEGIN
IF rx_idx < BUFSWORD THEN rx_buf[rx_idx]:= value;
inc(rx_idx);
IF rx_idx = BUFSWORD THEN write_sector(rx_buf);
IF rx_idx = 130 THEN
BEGIN
tx_buf[0] := 5;
tx_cnt := 1;
tx_idx:= -1;
rx_idx:= 0;
state:= diskCommand;
END;
END;
END;{case}
END;
FUNCTION diskty.read_: uint32_t;
VAR resu: uint32_t;
BEGIN
IF (tx_idx >= 0) AND (tx_idx < tx_cnt) THEN
BEGIN
resu := tx_buf[tx_idx];
END
ELSE
BEGIN
resu := 255;
END;
read_ := resu;
END;
PROCEDURE diskty.run_command;
VAR cmd: uint32_t;
arg: uint32_t;
(* myreadpos, mywritepos : longint; *)
BEGIN
cmd := rx_buf[0];
arg := (rx_buf[1] shl 24) or (rx_buf[2] shl 16) or (rx_buf[3] shl 8) or rx_buf[4];
CASE cmd OF
81: BEGIN
state:= diskRead;
tx_buf[0] := 0;
tx_buf[1] := 254;
(* myreadpos :=*) fileseek(myfile, (arg - offset) * BUFSBYTE, fsFromBeginning);
read_sector2(tx_buf);
tx_cnt:= 2 + BUFSWORD;
END;
88: BEGIN
state:= diskWrite;
(* mywritepos := *) fileseek(myfile, (arg - offset) * BUFSBYTE, fsFromBeginning);
tx_buf[0] := 0;
tx_cnt := 1;
END;
ELSE
BEGIN
tx_buf[0] := 0;
tx_cnt := 1;
END;
END;(*case*)
tx_idx := -1;
END;
PROCEDURE diskty.read_sector(VAR buffer : bufty);
VAR bytes: bytebufty;
i : 0..pred(BUFSWORD);
i2 : 0..pred(BUFSBYTE);
BEGIN
FOR i2 := 0 TO pred(BUFSBYTE) DO
BEGIN
bytes[i2] := 0;
END;
fileread(myfile, bytes, BUFSBYTE);
FOR i := 0 to Pred(BUFSWORD) DO
BEGIN
buffer[i]:= longword(bytes[i*4+0]) or (longword(bytes[i*4+1]) shl 8) or (longword(bytes[i*4+2]) shl 16) or (longword(bytes[i*4+3]) shl 24);
END;
END;
PROCEDURE diskty.read_sector2(VAR buffer : buftyp2);
VAR bytes : bytebufty;
i : 0..pred(BUFSWORD);
i2 : 0..pred(BUFSBYTE);
BEGIN
FOR i2 := 0 to pred(BUFSBYTE) DO
BEGIN
bytes[i2] := 0;
END;
fileread(myfile, bytes, BUFSBYTE);
FOR i := 0 to Pred(BUFSWORD) DO
BEGIN
buffer[i+2]:= longword(bytes[i*4+0]) or (longword(bytes[i*4+1]) shl 8) or (longword(bytes[i*4+2]) shl 16) or (longword(bytes[i*4+3]) shl 24);
END;
END;
PROCEDURE diskty.write_sector(buffer : bufty);
VAR bytes: bytebufty;
i : 0..pred(BUFSWORD);
BEGIN
FOR i := 0 to Pred(BUFSWORD) DO
BEGIN
bytes[i*4+0]:= byte((buffer[i]) and $FF);
bytes[i*4+1]:= byte((buffer[i] shr 8) and $FF);
bytes[i*4+2]:= byte((buffer[i] shr 16) and $FF);
bytes[i*4+3]:= byte((buffer[i] shr 24) and $FF);
END;
filewrite(myfile, bytes, BUFSBYTE);
END;
END.