Skip to content

Commit

Permalink
Merge branch 'develop' into feature/gcc-14-toolchain-update
Browse files Browse the repository at this point in the history
  • Loading branch information
roscopeco authored Oct 8, 2024
2 parents a58400e + c8ec0ba commit 84acad1
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 3 deletions.
79 changes: 76 additions & 3 deletions code/firmware/rosco_m68k_firmware/stage1/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@
#include "char_device.h"

#define CMD_IDENT ((uint8_t)0xf0)
#define CMD_LED_POWRED ((uint8_t)0x1)
#define CMD_LED_POWGRN ((uint8_t)0x2)
#define CMD_LED_POWBLU ((uint8_t)0x3)
#define CMD_LED_DISK ((uint8_t)0x5)
#define CMD_MODE_SET ((uint8_t)0x10)
#define CMD_MOUSE_STRM_OFF ((uint8_t)0x22)

#define CMD_ACK ((uint8_t)0xff)
#define IDENT_MODE_ASCII ((uint8_t)0x01)

#define CMD_MODE_SCAN ((uint8_t)0x00)
#define CMD_MODE_ASCII ((uint8_t)0x01)

#define TIMEOUT_TICKS 20

CharDevice keyboard_device;
Expand All @@ -27,7 +37,7 @@ static volatile long *ticks = (volatile long *)0x40c;
void INSTALL_KEYBOARD_HANDLERS(void);

static int try_get_char(CharDevice *device) {
long end = *ticks + 20;
long end = *ticks + TIMEOUT_TICKS;

while (*ticks < end) {
if (CHAR_DEV_CHECKCHAR_C(device)) {
Expand All @@ -38,6 +48,57 @@ static int try_get_char(CharDevice *device) {
return -1;
}

static inline __attribute__((always_inline)) bool try_send_command_0(uint8_t command, CharDevice *device) {
CHAR_DEV_SENDCHAR_C(command, device);

if (try_get_char(device) != CMD_ACK) {
#ifdef DEBUG_KEYBOARD_DETECT
FW_PRINT_C("Command 0x%02x [0x%02x] failed\r\n", command, argument);
#endif
return false;
}

return true;
}

static inline __attribute__((always_inline)) bool try_send_command_1(uint8_t command, uint8_t argument, CharDevice *device) {
CHAR_DEV_SENDCHAR_C(command, device);
CHAR_DEV_SENDCHAR_C(argument, device);

if (try_get_char(device) != CMD_ACK) {
#ifdef DEBUG_KEYBOARD_DETECT
FW_PRINT_C("Command 0x%02x [0x%02x] failed\r\n", command, argument);
#endif
return false;
}

return true;
}

static bool try_reset_keyboard(CharDevice *device) {
// Reset LEDs
if (!try_send_command_0(CMD_MOUSE_STRM_OFF, device)) {
return false;
}
if (!try_send_command_1(CMD_LED_POWRED, 0xff, device)) {
return false;
}
if (!try_send_command_1(CMD_LED_POWGRN, 0x00, device)) {
return false;
}
if (!try_send_command_1(CMD_LED_POWBLU, 0x00, device)) {
return false;
}
if (!try_send_command_1(CMD_LED_DISK, 0x00, device)) {
return false;
}
if (!try_send_command_1(CMD_MODE_SET, CMD_MODE_ASCII, device)) {
return false;
}

return true;
}

static bool detect_keyboard(CharDevice *device) {
// Clear buffer
while (CHAR_DEV_CHECKCHAR_C(device)) {
Expand All @@ -60,7 +121,7 @@ static bool detect_keyboard(CharDevice *device) {
}

chr = try_get_char(device);
if (chr != IDENT_MODE_ASCII) {
if (chr != CMD_MODE_ASCII && chr != CMD_MODE_SCAN) {
#ifdef DEBUG_KEYBOARD_DETECT
FW_PRINT_C("Bad mode\r\n");
#endif
Expand Down Expand Up @@ -115,6 +176,13 @@ static bool detect_keyboard(CharDevice *device) {
return false;
}

if (!try_reset_keyboard(device)) {
#ifdef DEBUG_KEYBOARD_DETECT
FW_PRINT_C("Reset failed\r\n");
#endif
return false;
}

#ifdef DEBUG_KEYBOARD_DETECT
FW_PRINT_C("Detect success\r\n");
#endif
Expand Down Expand Up @@ -146,4 +214,9 @@ void initialize_keyboard() {
FW_PRINT_C("No keyboard detected\r\n");
#endif
}

// Mop up any stray characters in case not a keyboard...
while (CHAR_DEV_CHECKCHAR_C(&keyboard_device)) {
CHAR_DEV_RECVCHAR_C(&keyboard_device);
}
}
21 changes: 21 additions & 0 deletions code/software/newlib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.PHONY: all clean

all: filetest.bin gdbtest.bin

clean:
$(MAKE) -C libc_gdb clean
$(MAKE) -C libc_sd clean
$(RM) *.bin

libc_sd/filetest.bin:
$(MAKE) -C libc_sd filetest.bin

filetest.bin: libc_sd/filetest.bin
cp $< $@

libc_gdb/gdbtest.bin:
$(MAKE) -C libc_gdb gdbtest.bin

gdbtest.bin: libc_gdb/gdbtest.bin
cp $< $@

5 changes: 5 additions & 0 deletions code/software/newlib/libc_gdb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
*.elf
*.bin
*.map

24 changes: 24 additions & 0 deletions code/software/newlib/libc_gdb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC=m68k-elf-rosco-gcc
OBJCOPY=m68k-elf-rosco-objcopy
OBJDUMP=m68k-elf-rosco-objdump
CFLAGS?=-g
LDFLAGS?=-g -mgdb

PROGRAM=gdbtest

all: $(PROGRAM).bin $(PROGRAM).dis

$(PROGRAM).elf: main.o
$(CC) $(LDFLAGS) -Wl,-Map=$(PROGRAM).map -Wl,--warn-common -o $@ $^

$(PROGRAM).dis: $(PROGRAM).elf
$(OBJDUMP) -d $< > $@

$(PROGRAM).bin: $(PROGRAM).elf
$(OBJCOPY) -O binary $< $@

clean:
$(RM) main.o $(PROGRAM).bin $(PROGRAM).map $(PROGRAM).elf $(PROGRAM).dis

.PHONY: all clean

22 changes: 22 additions & 0 deletions code/software/newlib/libc_gdb/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <rosco_m68k/debug.h>

#define BUF_SIZE 64

static void existing_file_test();
static void new_file_test();

static void afunc() {
int a = 3 + 5;
printf("Int is %d\n", a);
}

int main() {
printf("Waiting for debugger attach...\n");
breakpoint();

afunc();
}

5 changes: 5 additions & 0 deletions code/software/newlib/libc_sd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
*.elf
*.bin
*.map

24 changes: 24 additions & 0 deletions code/software/newlib/libc_sd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC=m68k-elf-rosco-gcc
OBJCOPY=m68k-elf-rosco-objcopy
OBJDUMP=m68k-elf-rosco-objdump
CFLAGS?=
LDFLAGS?=

PROGRAM=filetest

all: $(PROGRAM).bin $(PROGRAM).dis

$(PROGRAM).elf: main.o
$(CC) $(LDFLAGS) -Wl,-Map=$(PROGRAM).map -Wl,--warn-common -o $@ $^

$(PROGRAM).dis: $(PROGRAM).elf
$(OBJDUMP) -d $< > $@

$(PROGRAM).bin: $(PROGRAM).elf
$(OBJCOPY) -O binary $< $@

clean:
$(RM) main.o $(PROGRAM).bin $(PROGRAM).map $(PROGRAM).elf $(PROGRAM).dis

.PHONY: all clean

143 changes: 143 additions & 0 deletions code/software/newlib/libc_sd/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <rosco_m68k/debug.h>

#define BUF_SIZE 64

static void existing_file_test();
static void new_file_test();
static void list_directory(const char *path);

int main() {
printf("## libc file test\n\n");

existing_file_test();
new_file_test();

list_directory("/sd/");

FILE *f = fopen("/sd/gemsys", "r");
if (f) {
printf("fopen dir okay\n");
fclose(f);
} else {
printf("fopen dir not okay\n");
}


struct stat statbuf;
if (stat("/sd/gemsys", &statbuf) == 0) {
printf("Stat ok\n");
if (S_ISDIR(statbuf.st_mode)) {
printf("Is a dir\n");
} else {
printf("Is not a dir\n");
}

/*
int subdir_count = count_entries_in_directory(full_path);
if (subdir_count >= 0) {
printf(" --> Subdirectory '%s' contains %d entries\n", entry->d_name, subdir_count);
}
*/
} else {
printf("Stat not okay\n");
}

printf("Tests are done!\n");

breakpoint();
}

static void do_read_test(char *fn) {
FILE *f = fopen(fn, "r");

if (f) {
printf("File %s opened - reading\n", fn);
char buffer[BUF_SIZE];
int num;

do {
num = fread(buffer, 1, BUF_SIZE, f);

if (num > 0) {
printf("%.*s", num, buffer);
} else if (num == -1) {
printf(" Read failed: 0x%08x\n", errno);
} else {
if (feof(f)) {
printf("<--- EOF>\n");
} else {
printf("<--- ERR>\n");
}
}
} while (num > 0);
} else {
printf("Open failed: 0x%08x\n", errno);
}

fclose(f);
}

static void existing_file_test() {
printf("### Read existing file\n");
do_read_test("/sd/test.txt");
printf("Existing file test done\n\n");
}

static void new_file_test() {
printf("### Write / read new file\n");
FILE *f = fopen("/sd/test2.txt", "w");

if (f) {
printf("File /sd/test2.txt opened - writing\n");
char *buffer = "This is some data for a new file\nOf course, it is also rather awesome!\n\n";
int total = 0;
int num;

do {
num = fwrite(buffer, 1, strlen(buffer), f);

if (num > 0) {
total += num;
printf(" Wrote %d bytes [%d of total %d done]\n", num, total, strlen(buffer));
} else if (num == -1) {
printf(" Write failed: 0x%08x\n", errno);
break;
}
} while (total < strlen(buffer));
} else {
printf("Open failed: 0x%08x\n", errno);
}

fclose(f);

do_read_test("/sd/test2.txt");

printf("New file test done\n\n");
}

static void list_directory(const char *path) {
struct dirent *entry;
DIR *dp = opendir(path);

if (dp == NULL) {
perror("opendir");
return;
}

// Loop through directory entries
while ((entry = readdir(dp)) != NULL) {
// Ignore "." and ".." entries
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
printf("%s [DIR: %d]\n", entry->d_name, entry->d_is_dir);
}
}

closedir(dp);
}

0 comments on commit 84acad1

Please sign in to comment.