forked from fox32-arch/fox32rom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryfs.asm
539 lines (476 loc) · 11.2 KB
/
ryfs.asm
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
; RYFS routines
; file struct:
; file_disk: 1 byte
; file_first_sector: 2 bytes
; file_seek_offset: 4 bytes
; file_reserved: 1 byte
; open a file from a RYFS-formatted disk
; inputs:
; r0: pointer to file name string (8.3 format, for example "test txt" for test.txt)
; r1: disk ID
; r2: file struct: pointer to a blank file struct
; outputs:
; r0: first file sector, or zero if file wasn't found
ryfs_open:
push r1
push r2
push r10
push r31
; check if the requested disk id is inserted
cmp r1, 4
ifnz jmp ryfs_open_check_disk
ifz call is_romdisk_available
ifnz jmp ryfs_open_fail
cmp r1, 4
ifz jmp ryfs_open_continue
ryfs_open_check_disk:
mov r10, r1
or r10, 0x80001000
in r10, r10
cmp r10, 0
ifz jmp ryfs_open_fail
ryfs_open_continue:
; r10: pointer to file struct entry
mov r10, r2
mov.8 [r10], r1 ; write file_disk
inc r10
push r0
mov r0, 1
mov r2, TEMP_SECTOR_BUF
call read_sector
pop r0
; point to the file name of the first directory entry
mov r1, TEMP_SECTOR_BUF
add r1, 20
mov r2, 11 ; compare 11 bytes
mov r31, 31 ; 31 file entries total
ryfs_open_find_dir_entry_loop:
call compare_memory_bytes
ifz jmp ryfs_open_found_dir_entry
add r1, 16 ; point to the file name in the next directory entry
loop ryfs_open_find_dir_entry_loop
; if we reach this point, the file wasn't found
jmp ryfs_open_fail
ryfs_open_found_dir_entry:
sub r1, 4 ; point to first sector of this file
mov.16 [r10], [r1] ; write file_first_sector
add r10, 2
mov [r10], 0 ; write file_seek_offset
inc r10, 4
mov.8 [r10], 0 ; write file_reserved
movz.16 r0, [r1]
ryfs_open_end:
pop r31
pop r10
pop r2
pop r1
ret
ryfs_open_fail:
mov r0, 0
jmp ryfs_open_end
; seek specified file to the specified offset
; inputs:
; r0: byte offset
; r1: pointer to file struct
; outputs:
; none
ryfs_seek:
push r1
add r1, 3
mov [r1], r0
pop r1
ret
; get the seek offset of the specified file
; inputs:
; r0: pointer to file struct
; outputs:
; r0: byte offset
ryfs_tell:
add r0, 3
mov r0, [r0]
ret
; read specified number of bytes into the specified buffer
; inputs:
; r0: number of bytes to read
; r1: pointer to file struct
; r2: pointer to destination buffer
; outputs:
; none
ryfs_read:
push r0
push r1
push r2
push r3
push r4
push r5
push r10
push r11
push r12
push r31
; number_of_sectors_to_load = ceil(input r0, 506) / 506
; first, ceil the number of bytes to multiple of 506
; value_temp = value / 506
; if value % 506 != 0:
; value_temp++
; value_ceil = value_temp * 506
mov r10, r0
mov r11, r0
mov r12, 506
div r11, r12
rem r10, r12
ifnz inc r11
mul r11, r12
div r11, r12
mov r3, r11 ; r3: number_of_sectors_to_load
mov r10, r1
add r10, 3
mov r4, [r10]
mov r10, r4
mov r11, r4
mov r12, 506
div r11, r12
rem r10, r12
ifnz inc r11
mul r11, r12
div r11, r12
mov r4, r11 ; r4: number of sectors to traverse
; start_sector = traverse through linked sectors starting at file_struct.file_first_sector
push r0
push r1
push r2
mov r10, r1
; read the file's first sector into the temp buffer
movz.8 r1, [r10] ; file_disk
inc r10
movz.16 r0, [r10] ; file_first_sector
mov r31, r4
ryfs_read_traverse_sectors_loop:
mov r2, TEMP_SECTOR_BUF
call read_sector
mov r4, r0
add r2, 2 ; point to next sector number
movz.16 r0, [r2] ; load next sector number
cmp r31, 0 ; if we started at zero, then don't attempt to loop
ifnz loop ryfs_read_traverse_sectors_loop
pop r2
pop r1
pop r0
; r4: start_sector
; total_bytes_remaining = input r0
; this_sector = start_sector
; for range 0..number_of_sectors_to_load:
; load this_sector into temporary buffer
; bytes_to_load = if total_bytes_remaining >= 506
; 506
; else:
; total_bytes_remaining
; for range 0..bytes_to_load:
; load byte from temporary buffer into destination buffer
; this_sector = this_sector.next
mov r10, r0 ; r10: total_bytes_remaining
ryfs_read_sector_loop:
push r0
push r1
push r2
; read this sector into the temp buffer
mov r0, r4
movz.8 r1, [r1] ; file_disk
mov r2, TEMP_SECTOR_BUF
call read_sector
pop r2
pop r1
pop r0
; r11: bytes_to_load
cmp r10, 506
ifgteq mov r11, 506
iflt mov r11, r10
push r0
push r1
push r2
; calculate the seek offset
mov r12, r1
add r12, 3
mov r5, [r12]
rem r5, 506
; copy the sector data to the destination buffer
mov r0, TEMP_SECTOR_BUF
add r0, 6
add r0, r5 ; add the seek offset
mov r1, r2
mov r2, r11
call copy_memory_bytes
pop r2
pop r1
pop r0
; this_sector = this_sector.next
mov r5, TEMP_SECTOR_BUF
add r5, 2
movz.16 r5, [r5]
mov r4, r5
add r2, r11
sub r10, r11
ifnz jmp ryfs_read_sector_loop
; file_struct.file_seek_offset += input r0
add r1, 3
add [r1], r0
pop r31
pop r12
pop r11
pop r10
pop r5
pop r4
pop r3
pop r2
pop r1
pop r0
ret
; read a whole file into the specified buffer
; inputs:
; r0: pointer to file struct
; r1: pointer to destination buffer
; outputs:
; none
ryfs_read_whole_file:
push r0
push r1
push r2
mov r2, r1
mov r1, r0
call ryfs_get_size
call ryfs_read
pop r2
pop r1
pop r0
ret
; write specified number of bytes from the specified buffer
; THIS DOES NOT UPDATE THE TOTAL SIZE OF THE FILE; do not write past the end of a file, bad things will happen
; inputs:
; r0: number of bytes to write
; r1: pointer to file struct
; r2: pointer to source buffer
; outputs:
; none
ryfs_write:
push r0
push r1
push r2
push r3
push r4
push r5
push r10
push r11
push r12
push r31
; number_of_sectors_to_load = ceil(input r0, 506) / 506
; first, ceil the number of bytes to multiple of 506
; value_temp = value / 506
; if value % 506 != 0:
; value_temp++
; value_ceil = value_temp * 506
mov r10, r0
mov r11, r0
mov r12, 506
div r11, r12
rem r10, r12
ifnz inc r11
mul r11, r12
div r11, r12
mov r3, r11 ; r3: number_of_sectors_to_load
mov r10, r1
add r10, 3
mov r4, [r10]
mov r10, r4
mov r11, r4
mov r12, 506
div r11, r12
rem r10, r12
ifnz inc r11
mul r11, r12
div r11, r12
mov r4, r11 ; r4: number of sectors to traverse
; start_sector = traverse through linked sectors starting at file_struct.file_first_sector
push r0
push r1
push r2
mov r10, r1
; read the file's first sector into the temp buffer
movz.8 r1, [r10] ; file_disk
inc r10
movz.16 r0, [r10] ; file_first_sector
mov r31, r4
ryfs_write_traverse_sectors_loop:
mov r2, TEMP_SECTOR_BUF
call read_sector
mov r4, r0
add r2, 2 ; point to next sector number
movz.16 r0, [r2] ; load next sector number
cmp r31, 0 ; if we started at zero, then don't attempt to loop
ifnz loop ryfs_write_traverse_sectors_loop
pop r2
pop r1
pop r0
; r4: start_sector
; total_bytes_remaining = input r0
; this_sector = start_sector
; for range 0..number_of_sectors_to_load:
; load this_sector into temporary buffer
; bytes_to_load = if total_bytes_remaining >= 506
; 506
; else:
; total_bytes_remaining
; for range 0..bytes_to_load:
; load byte from temporary buffer into destination buffer
; this_sector = this_sector.next
mov r10, r0 ; r10: total_bytes_remaining
ryfs_write_sector_loop:
push r0
push r1
push r2
; read this sector into the temp buffer
mov r0, r4
movz.8 r1, [r1] ; file_disk
mov r2, TEMP_SECTOR_BUF
call read_sector
pop r2
pop r1
pop r0
; r11: bytes_to_load
cmp r10, 506
ifgteq mov r11, 506
iflt mov r11, r10
push r0
push r1
push r2
; calculate the seek offset
mov r12, r1
add r12, 3
mov r5, [r12]
rem r5, 506
; copy the sector data from the source buffer to the temp buffer
mov r0, r2
mov r1, TEMP_SECTOR_BUF
add r1, 6
add r1, r5 ; add the seek offset
mov r2, r11
call copy_memory_bytes
pop r2
pop r1
pop r0
push r0
push r1
push r2
; write this sector back out to disk
mov r0, r4
movz.8 r1, [r1] ; file_disk
mov r2, TEMP_SECTOR_BUF
call write_sector
pop r2
pop r1
pop r0
; this_sector = this_sector.next
mov r5, TEMP_SECTOR_BUF
add r5, 2
movz.16 r5, [r5]
mov r4, r5
add r2, r11
sub r10, r11
ifnz jmp ryfs_write_sector_loop
; file_struct.file_seek_offset += input r0
add r1, 3
add [r1], r0
pop r31
pop r12
pop r11
pop r10
pop r5
pop r4
pop r3
pop r2
pop r1
pop r0
ret
; get the exact size of a file
; inputs:
; r0: pointer to file struct
; outputs:
; r0: file size in bytes
ryfs_get_size:
push r1
push r2
push r10
push r11
mov r10, r0
mov r11, 0 ; byte counter
; read the first sector into the temp buffer
movz.8 r1, [r0] ; file_disk
inc r0
movz.16 r0, [r0] ; file_first_sector
ryfs_get_size_sector_loop:
mov r2, TEMP_SECTOR_BUF
call read_sector
; check to see if this is the last sector
; if this is the last sector, read the sector size field in the header
mov r0, TEMP_SECTOR_BUF
add r0, 2
cmp.16 [r0], 0
ifz jmp ryfs_get_size_last_sector
; there are more sectors left, load them
movz.16 r0, [r0] ; sector number
mov r1, [r10] ; file_disk
add r11, 506
jmp ryfs_get_size_sector_loop
ryfs_get_size_last_sector:
; this is the last sector, read the header to get the reamining number of bytes
mov r0, TEMP_SECTOR_BUF
add r0, 4
movz.16 r0, [r0]
add r11, r0
; return final size
mov r0, r11
pop r11
pop r10
pop r2
pop r1
ret
; get a list of all files on disk
; format: "file1 extfile2 ext", etc.
; each file name is exactly 11 characters long, the buffer is not null-terminated
; inputs:
; r0: pointer to 341 byte buffer (max size) for file names (31 file entries * 11 chars)
; r1: disk ID
; outputs:
; r0: number of files
ryfs_get_file_list:
push r1
push r2
push r3
push r31
mov r3, r0
; load directory sector into temp buffer
mov r0, 1
mov r2, TEMP_SECTOR_BUF
call read_sector
mov r31, 31
mov r0, TEMP_SECTOR_BUF
add r0, 20 ; point to first file name
mov r1, r3
mov r3, 0 ; file counter
ryfs_get_file_list_loop:
; check if this file entry is filled
cmp.8 [r0], 0
ifz jmp ryfs_get_file_list_loop_next
; copy 11 bytes from [r0] to [r1]
mov r2, 11
call copy_memory_bytes
inc r3
ryfs_get_file_list_loop_next:
add r0, 16 ; point to next file entry
add r1, 11 ; increment the destination pointer
loop ryfs_get_file_list_loop
; return number of files
mov r0, r3
pop r31
pop r3
pop r2
pop r1
ret