Add some basic code, build kernel stuff in tree
This commit is contained in:
1
sw/fsdir/test.txt
Normal file
1
sw/fsdir/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
This is a text file, which is composed of text.
|
||||
@@ -5,6 +5,12 @@
|
||||
|
||||
void fat32_init();
|
||||
|
||||
extern uint32_t root_cluster;
|
||||
extern uint16_t fat_start_sector;
|
||||
extern uint32_t data_start_sector;
|
||||
extern uint32_t fat_size;
|
||||
extern uint8_t* sd_buf;
|
||||
|
||||
struct fat32_directory_entry {
|
||||
char file_name[8];
|
||||
char file_ext[3];
|
||||
@@ -31,4 +37,7 @@ struct lfn_entry {
|
||||
uint16_t name_2[2];
|
||||
};
|
||||
|
||||
uint8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry);
|
||||
uint8_t fat32_read_cluster(uint32_t cluster, void* buf);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,14 +9,16 @@
|
||||
|
||||
.export _fat32_init
|
||||
|
||||
.export _root_cluster, _fat_start_sector, _data_start_sector, _fat_size, _sd_buf
|
||||
|
||||
|
||||
.data
|
||||
root_cluster: .res 4
|
||||
fat_start_sector: .res 2
|
||||
data_start_sector: .res 4
|
||||
fat_size: .res 4
|
||||
_root_cluster: .res 4
|
||||
_fat_start_sector: .res 2
|
||||
_data_start_sector: .res 4
|
||||
_fat_size: .res 4
|
||||
|
||||
sd_buf: .res 512
|
||||
_sd_buf: .res 512
|
||||
|
||||
data_start_sect_str: .asciiz "Data sector start: 0x%lx\n"
|
||||
starting_cluster_str: .asciiz "Root cluster num: %lx\n";
|
||||
@@ -24,12 +26,12 @@ value_str: .asciiz "Value: 0x%x\n"
|
||||
|
||||
.code
|
||||
|
||||
bytes_per_sector = sd_buf + $0B
|
||||
sectors_per_cluster = sd_buf + $0D
|
||||
reserved_sectors = sd_buf + $0E
|
||||
fat_count = sd_buf + $10
|
||||
sectors_per_fat = sd_buf + $24
|
||||
root_cluster_offs = sd_buf + $2C
|
||||
bytes_per_sector = _sd_buf + $0B
|
||||
sectors_per_cluster = _sd_buf + $0D
|
||||
reserved_sectors = _sd_buf + $0E
|
||||
fat_count = _sd_buf + $10
|
||||
sectors_per_fat = _sd_buf + $24
|
||||
root_cluster_offs = _sd_buf + $2C
|
||||
|
||||
.proc _fat32_init
|
||||
; load sector 0 into sd_buf
|
||||
@@ -38,8 +40,8 @@ root_cluster_offs = sd_buf + $2C
|
||||
stz sreg
|
||||
stz sreg+1
|
||||
jsr pusheax
|
||||
lda #<sd_buf
|
||||
ldx #>sd_buf
|
||||
lda #<_sd_buf
|
||||
ldx #>_sd_buf
|
||||
jsr pushax
|
||||
lda #<ptr1
|
||||
ldx #>ptr1
|
||||
@@ -47,7 +49,7 @@ root_cluster_offs = sd_buf + $2C
|
||||
|
||||
ldx #$00
|
||||
L1: lda root_cluster_offs,x
|
||||
sta root_cluster,x
|
||||
sta _root_cluster,x
|
||||
inx
|
||||
cpx #$4
|
||||
blt L1
|
||||
@@ -60,8 +62,8 @@ L1: lda root_cluster_offs,x
|
||||
jsr _imulii
|
||||
txa
|
||||
lsr
|
||||
sta fat_start_sector
|
||||
stz fat_start_sector + 1
|
||||
sta _fat_start_sector
|
||||
stz _fat_start_sector + 1
|
||||
|
||||
; multiply fat size and number of fats to get total fat size
|
||||
lda fat_count
|
||||
@@ -69,53 +71,25 @@ L1: lda root_cluster_offs,x
|
||||
lda sectors_per_fat
|
||||
ldx sectors_per_fat+1
|
||||
jsr _lmulii
|
||||
sta fat_size
|
||||
stx fat_size+1
|
||||
sta _fat_size
|
||||
stx _fat_size+1
|
||||
lda sreg
|
||||
sta fat_size+2
|
||||
sta _fat_size+2
|
||||
lda sreg+1
|
||||
sta fat_size+3
|
||||
sta _fat_size+3
|
||||
|
||||
|
||||
; Add fat size to starting fat sector to get data start sector
|
||||
lda fat_size
|
||||
adc fat_start_sector
|
||||
sta data_start_sector
|
||||
lda fat_size+1
|
||||
adc fat_start_sector+1
|
||||
sta data_start_sector+1
|
||||
lda fat_size+2
|
||||
sta data_start_sector+2
|
||||
lda fat_size+3
|
||||
sta data_start_sector+3
|
||||
|
||||
lda #<data_start_sect_str
|
||||
ldx #>data_start_sect_str
|
||||
jsr pushax
|
||||
lda data_start_sector+2
|
||||
sta sreg
|
||||
lda data_start_sector+3
|
||||
sta sreg+1
|
||||
lda data_start_sector
|
||||
ldx data_start_sector+1
|
||||
jsr pusheax
|
||||
ldy #$6
|
||||
jsr _cprintf
|
||||
|
||||
; load sector <data_start> into sd_buf
|
||||
lda data_start_sector+2
|
||||
sta sreg
|
||||
lda data_start_sector+3
|
||||
sta sreg+1
|
||||
lda data_start_sector
|
||||
ldx data_start_sector+1
|
||||
jsr pusheax
|
||||
lda #<sd_buf
|
||||
ldx #>sd_buf
|
||||
jsr pushax
|
||||
lda #<ptr1
|
||||
ldx #>ptr1
|
||||
jsr _SD_readSingleBlock
|
||||
lda _fat_size
|
||||
adc _fat_start_sector
|
||||
sta _data_start_sector
|
||||
lda _fat_size+1
|
||||
adc _fat_start_sector+1
|
||||
sta _data_start_sector+1
|
||||
lda _fat_size+2
|
||||
sta _data_start_sector+2
|
||||
lda _fat_size+3
|
||||
sta _data_start_sector+3
|
||||
|
||||
rts
|
||||
.endproc
|
||||
|
||||
26
sw/kernel/filesystems/fat32_c.c
Normal file
26
sw/kernel/filesystems/fat32_c.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "fat32.h"
|
||||
#include <devices/sd_card.h>
|
||||
#include <conio.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint8_t fat32_read_cluster(uint32_t cluster, void* buf) {
|
||||
uint8_t error;
|
||||
uint32_t addr = (cluster - 2) + data_start_sector;
|
||||
SD_readSingleBlock(addr, buf, &error);
|
||||
return error;
|
||||
}
|
||||
|
||||
uint8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentry) {
|
||||
struct fat32_directory_entry* local_entry;
|
||||
int i = 0;
|
||||
fat32_read_cluster(root_cluster, sd_buf);
|
||||
for (i = 0; i < 16; i++){
|
||||
local_entry = sd_buf + i*32;
|
||||
if (local_entry->attr1 == 0xf || local_entry->attr1 & 0x8 || !local_entry->attr1) {
|
||||
continue;
|
||||
}
|
||||
cprintf("Name: %.11s\n", local_entry->file_name, local_entry->file_ext);
|
||||
cprintf("attr1: %x\n", local_entry->attr1);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
CC=../../cc65/bin/cl65
|
||||
LD=../../cc65/bin/cl65
|
||||
SIM=../../cc65/bin/sim65
|
||||
CFLAGS=-T -t sim65c02 -I.
|
||||
CFLAGS=-T -t sim65c02 -I. -I $(REPO_TOP)/sw/kernel
|
||||
LDFLAGS=-m $(NAME).map
|
||||
|
||||
NAME=fs_test
|
||||
@@ -14,17 +14,18 @@ FS=$(REPO_TOP)/sw/script/fs.fat
|
||||
|
||||
LISTS=lists
|
||||
|
||||
EXT_SRCS=$(REPO_TOP)/sw/kernel/filesystems/fat32.s
|
||||
EXT_SRCS=$(REPO_TOP)/sw/kernel/filesystems/fat32.s $(REPO_TOP)/sw/kernel/filesystems/fat32_c.c
|
||||
|
||||
SRCS=$(wildcard *.s) $(wildcard *.c)
|
||||
SRCS+=$(wildcard **/*.s) $(wildcard **/*.c)
|
||||
SRCS+=$(EXT_SRCS)
|
||||
OBJS+=$(patsubst %.s,%.o,$(filter %s,$(SRCS)))
|
||||
OBJS+=$(patsubst %.c,%.o,$(filter %c,$(SRCS)))
|
||||
|
||||
run: all
|
||||
$(SIM) $(SIMARGS) $(BIN)
|
||||
|
||||
all: fs.fat fat32.s $(BIN)
|
||||
all: fs.fat $(BIN)
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@
|
||||
@@ -35,16 +36,14 @@ $(BIN): $(OBJS)
|
||||
%.o: %.s $(LISTS)
|
||||
$(CC) $(CFLAGS) -l $(LISTS)/$<.list -c $< -o $@
|
||||
|
||||
fat32.s: $(EXT_SRCS)
|
||||
cp $^ .
|
||||
|
||||
fs.fat: $(FS)
|
||||
cp $^ .
|
||||
|
||||
|
||||
$(LISTS):
|
||||
mkdir -p $(addprefix $(LISTS)/,$(sort $(dir $(SRCS))))
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf $(OBJS) $(BIN) $(HEX) $(LISTS) $(NAME).map fat32.s
|
||||
rm -rf $(OBJS) $(BIN) $(HEX) $(LISTS) $(NAME).map
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <filesystems/fat32.h>
|
||||
|
||||
void fat32_init(void);
|
||||
|
||||
int main(void) {
|
||||
struct fat32_directory_entry dentry;
|
||||
fat32_init();
|
||||
fat32_get_cluster_by_name("KERNEL O65", &dentry);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user