Skip to content

Commit

Permalink
[commodore] Implement KERNAL LOAD function as inline assembly (#373)
Browse files Browse the repository at this point in the history
* Rewrite cbm_k_load w. inline asm

* Add doxygen documentation

* Remove empty line

* Update doxygen documentation
  • Loading branch information
mlund authored Sep 19, 2024
1 parent 34121b7 commit 5138223
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion mos-platform/commodore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ add_platform_library(commodore-c
cbm_k_getin.c
cbm_k_iobase.s
cbm_k_listen.c
cbm_k_load.s
cbm_k_load.c
cbm_k_open.s
cbm_k_readst.c
cbm_k_save.s
Expand Down
24 changes: 23 additions & 1 deletion mos-platform/commodore/cbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,29 @@ void cbm_k_clrch (void);
unsigned char cbm_k_getin (void);
unsigned cbm_k_iobase (void) __attribute__((leaf));
void cbm_k_listen (unsigned char dev);
void *cbm_k_load(unsigned char flag, void *startaddr) __attribute__((leaf));

/**
* @brief CBM KERNAL function to load or verify file
*
* Should be called after `cbm_k_setlfs()` and `cbm_k_setnam()`.
* On Commander X16, the `flag` argument takes the following
* values:
*
* `flag` | Description
* ------ | ---------------
* 0 | Load at address
* 1 | Verify
* 2 | Loads into VRAM $00000 + address (cx16)
* 3 | Loads into VRAM $10000 + address (cx16)
*
* On other Commodore targets, a non-zero `flag` value triggers verify.
*
* @param flag Load or verify
* @param load_addr Load address pointer (if not verifying).
* @return Address of last byte loaded/verified or KERNAL error code
*/
void *cbm_k_load(unsigned char flag, void *load_addr);

unsigned char cbm_k_open (void) __attribute__((leaf));
unsigned char cbm_k_readst (void);
unsigned char cbm_k_save(void *startaddr, void *endaddr_plusone) __attribute__((leaf));
Expand Down
31 changes: 31 additions & 0 deletions mos-platform/commodore/cbm_k_load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 LLVM-MOS Project
// Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
// See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
// information.

#include <stdint.h>

// Helper to (dis)assemble a 16-bit integer
typedef union {
uint16_t value;
struct {
uint8_t lo, hi;
};
} Word;

void *cbm_k_load(const unsigned char flag, void *load_addr) {
Word result;
const Word addr = {.value = (uint16_t)(load_addr)};

__attribute__((leaf)) __asm__ volatile(
" jsr __LOAD \n"
" bcc 1f \n" // no error if carry clear
" tax \n" // else get error code from A
" ldy #0 \n"
"1: \n" // no error
: "=x"(result.lo), "=y"(result.hi)
: "a"(flag), "x"(addr.lo), "y"(addr.hi)
: "p");

return (void *)(result.value);
}
23 changes: 0 additions & 23 deletions mos-platform/commodore/cbm_k_load.s

This file was deleted.

0 comments on commit 5138223

Please sign in to comment.