new File device changes

This commit is contained in:
Andrew Alderwick 2021-11-06 10:37:07 +00:00
parent 35879aaa47
commit ca89f1b5ed
1 changed files with 5 additions and 15 deletions

View File

@ -13,16 +13,18 @@ the file device in the varvara computer allows us to read and write to external
its ports are normally defined as follows:
```
|a0 @File [ &vector $2 &success $2 &offset-hs $2 &offset-ls $2 &name $2 &length $2 &load $2 &save $2 ]
|a0 @File [ &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &load $2 &save $2 ]
```
* the vector short is currently unused
* the success short stores the length of the data that was successfully read or written, or zero if there was an error
* offset-hs and offset-ls are a couple of shorts that compose a 32-bits number corresponding to an offset for the reading or writing of a file: how many bytes should the operation skip from the beginning of the file.
* the name short is for the memory address where the filename (null-terminated, i.e. with a 00) is stored
* the length short is the amount of bytes to read or write: don't forget that the program memory is ffff plus 1 bytes long, and that the program itself is stored there!
* the load short is for the starting memory address where the read data should be stored
* the save short is for the starting memory address where the data to be written is stored
* the stat short is similar to load, but reads the directory entry for the filename
* the delete byte deletes the file when any value is written to it
* setting the append byte to 01 makes save append data to the end of the file, when it is the default, 00, save overwrites the contents from the start
a read operation is started when the load short is written to, and a write operation is started when the save short is written to.
@ -82,7 +84,7 @@ in order to write a file, we need:
* the amount of bytes that we want to write into the file
* the label for the section of program memory that we will write into the file
keep in mind that the file will be completely overwritten unless you set some offset!
keep in mind that the file will be completely overwritten unless you set append to 01!
the following program will write "hello" and a newline (0a) into a file called "test.txt":
@ -114,18 +116,6 @@ note how similar it is to the load-file subroutine!
the only differences, beside the use of File/save instead of File/load, are the file length and the comparison for the success short: in this case we know for sure how many bytes should have been written.
## offsets
if we wanted to read or write our file starting from a specific position (in bytes) in it, different from its beginning, we'd need to set the File/offset-ls short, and very rarely (i guess) the File/offset-hs short.
for example, if we wanted to continue writing to our file after the "hello" and newline, we could set:
```
#0006 .File/offset-ls DEO2
```
and then the next write will not overwrite the file, but append to it starting at position 6 (i.e. just after the newline)
## a brief case study: the theme file
programs for the varvara computer written by 100r tend to have the ability to read a "theme" file that contains six bytes corresponding to the three shorts for the system colors.