Skip to content

Commit

Permalink
Add template based bank switch proxy code
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Oct 2, 2024
1 parent f6c78d5 commit a71fae5
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/c64/easyflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,46 @@ struct EasyFlash

#define eflash (*(EasyFlash *)0xde00)

#ifdef __cplusplus

#ifdef EFPROX_SECTION
#pragma code(EFPROX_SECTION)
#endif

template<int back, class fn, class ... P>
__noinline auto ef_call_p(P... p)
{
if (back != __bankof(fn))
eflash.bank = __bankof(fn);
auto r = fn(p...);
if (back != 0xff && back != __bankof(fn))
eflash.bank = back;
return r;
}

#ifdef EFPROX_SECTION
#pragma code(code)
#endif

template<class fn>
class EFlashCall
{
public:
template<class ... P>
__forceinline auto operator()(P ... p) const
{
switch(__bankof(0))
{
#for(i,64) case i: return ef_call_p<i, fn, P...>(p...);
default:
return ef_call_p<0xff, fn, P...>(p...);
}
}
};

#define EF_CALL(fn) EFlashCall<fn##_p> fn

#endif

#endif

1 change: 1 addition & 0 deletions samples/memmap/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
../../bin/oscar64 easyflash.c -n -tf=crt
../../bin/oscar64 easyflashreloc.c -n -tf=crt
../../bin/oscar64 easyflashshared.c -n -tf=crt
../../bin/oscar64 easyflashcall.cpp -n -tf=crt
../../bin/oscar64 tsr.c -n -dNOFLOAT -dNOLONG
../../bin/oscar64 overlay.c -n -d64=overlay.d64
158 changes: 158 additions & 0 deletions samples/memmap/easyflashcall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <c64/memmap.h>
#include <c64/charwin.h>
#include <c64/cia.h>
#include <c64/vic.h>
#include <c64/easyflash.h>

// Shared code/data region, copied from easyflash bank 0 to ram during startup

#pragma region( main, 0x0900, 0x8000, , , { code, data, bss, heap, stack } )

// Section and region for first easyflash bank

#pragma section( bcode1, 0 )
#pragma section( bdata1, 0 )
#pragma region(bank1, 0x8000, 0xc000, , 1, { bcode1, bdata1 } )

// Section and region for second easyflash bank

#pragma section( bcode2, 0 )
#pragma section( bdata2, 0 )
#pragma region(bank2, 0x8000, 0xc000, , 2, { bcode2, bdata2 } )

#pragma section( bcode3, 0 )
#pragma section( bdata3, 0 )
#pragma region(bank3, 0x8000, 0xc000, , 3, { bcode3, bdata3 } )

#pragma section( bcode4, 0 )
#pragma section( bdata4, 0 )
#pragma region(bank4, 0x8000, 0xc000, , 4, { bcode4, bdata4 } )

#pragma section( bcode5, 0 )
#pragma section( bdata5, 0 )
#pragma region(bank5, 0x8000, 0xc000, , 5, { bcode5, bdata5 } )

#pragma section( bcode6, 0 )
#pragma section( bdata6, 0 )
#pragma region(bank6, 0x8000, 0xc000, , 6, { bcode6, bdata6 } )

// Charwin in shared memory section

CharWin cw;

// Now switch code generation to bank 1

#pragma code ( bcode1 )
#pragma data ( bdata1 )

// Print into shared charwin

void print1_p(void)
{
cwin_put_string(&cw, p"This is first bank", 7);
cwin_cursor_newline(&cw);
}

// Now switch code generation to bank 2

#pragma code ( bcode2 )
#pragma data ( bdata2 )

void print2_p(const char * p)
{
cwin_put_string(&cw, p"This is second bank:", 7);
cwin_put_string(&cw, p, 1);
cwin_cursor_newline(&cw);
}

#pragma code ( bcode3 )
#pragma data ( bdata3 )

void print3_p(void)
{
cwin_put_string(&cw, p"This is third bank", 7);
cwin_cursor_newline(&cw);
}

#pragma code ( bcode4 )
#pragma data ( bdata4 )

void print4_p(int x, int y)
{
cwin_cursor_move(&cw, x, y);
cwin_put_string(&cw, p"This is fourth bank", 7);
cwin_cursor_newline(&cw);
}

#pragma code ( bcode5 )
#pragma data ( bdata5 )

void print5_p(void)
{
cwin_put_string(&cw, p"This is fifth bank", 7);
cwin_cursor_newline(&cw);
}

void print5a_p(void)
{
cwin_put_string(&cw, p"This is fifth bank second", 14);
cwin_cursor_newline(&cw);
}

// Switching code generation back to shared section

#pragma code ( code )
#pragma data ( data )


EF_CALL(print1);
EF_CALL(print2);
EF_CALL(print3);
EF_CALL(print4);
EF_CALL(print5);
EF_CALL(print5a);

#pragma code ( bcode6 )
#pragma data ( bdata6 )

void print6_p(void)
{
cwin_put_string(&cw, p"This is sixth bank", 7);
cwin_cursor_newline(&cw);
print5a();
cwin_put_string(&cw, p"This is sixth bank again", 7);
}

#pragma code ( code )
#pragma data ( data )

EF_CALL(print6);

int main(void)
{
// Enable ROM
mmap_set(MMAP_ROM);

// Init CIAs (no kernal rom was executed so far)
cia_init();

// Init VIC
vic_setmode(VICM_TEXT, (char *)0x0400, (char *)0x1800);

// Prepare output window
cwin_init(&cw, (char *)0x0400, 0, 0, 40, 25);
cwin_clear(&cw);

print1();
print2("hello");
print3();
print4(5, 8);
print5();
print6();

// Loop forever
while (true)
;

return 0;
}
1 change: 1 addition & 0 deletions samples/memmap/make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ call ..\..\bin\oscar64 easyflash.c -n -tf=crt
call ..\..\bin\oscar64 easyflashreloc.c -n -tf=crt
call ..\..\bin\oscar64 easyflashshared.c -n -tf=crt
call ..\..\bin\oscar64 easyflashlow.c -n -tf=crt
call ..\..\bin\oscar64 easyflashcall.cpp -n -tf=crt
call ..\..\bin\oscar64 tsr.c -n -dNOFLOAT -dNOLONG
call ..\..\bin\oscar64 overlay.c -n -d64=overlay.d64
call ..\..\bin\oscar64 magicdesk.c -n -tf=crt8 -cid=19

0 comments on commit a71fae5

Please sign in to comment.