Jump to content
IGNORED

lseek and read for cc65 compiler


karri

Recommended Posts

Hi,

 

I spent two evenings putting together a very minimal set of standard tools for accessing the cart. They will be included in the cc65 libraries soon.

 

The first one is

 

off_t lseek(int fd, off_t offset, int whence);

 

Of this I implemented only seek to a 32-bit offset. This code just sets the Lynx cart hardware to the right position so that the next read command can access the right bytes.

 

Example:

 

lseek(1, 0, SEEK_SET); // Seek to the 1st byte on the cart

lseek(1, 1234, SEEK_SET); // Seek to the 1234:th byte on the cart

 

The lseek-command will ignore the fd-parameters and whence-parameter completely. This is because the Lynx only has one set of registers for accessing the cart. So you can not open two files at the same time anyway.

 

The second command to implement is

 

size_t read(int fd, void *buf, size_t count);

 

This will read "count" bytes into buf starting at the place where lseek or a previous read has left the hardware.

 

Example:

 

char buf[10];
lseek(1, 0, SEEK_SET);
read(1, buf, 10); // Read first 10 bytes into buf
read(1, buf, 10); // Read next 10 bytes into buf

 

The commands lseek and read will operate on the raw cart directly. They do not understand anything about files.

 

For files I plan to implement a command "open".

 

int open(const char *pathname, int flags, ...);

 

This command would understand filenames "0", "1", "2", ...

Internally this open-command would work like this:

 

int open(char name, int flags)
{
 int fileno = atoi(name);
 lseek(1, StartOfDirectory + fileno * 8, SEEK_SET); // Go to correct dir entry
 read(1, FileEntry, ; // Read entry into memory
 block(FileStartBlock); // Set the Lynx address hardware to point to the content of this file
 lynxskip(FileBlockOffset);
 return 1;
}

 

So after running "open" you have the directory entry available in the FileEntry structure.

FileEntry:
FileStartBlock: byte
FileBlockOffset: int
FileExecFlag: byte
FileDestAddr: int
FileFileLen: int

 

Example of how to load a segment into memory.

 

void load(char name) {
 open(name, O_RDONLY);
 read(1, FileDestAddr, FileFileLen);
}

load("123"); // Load file number 123 into RAM

 

--

Seasons Greetings,

 

Karri

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...