Skip to content

Commit

Permalink
c64: set_raster_irq(), wait_vblank()
Browse files Browse the repository at this point in the history
  • Loading branch information
sehugg committed Nov 29, 2023
1 parent d9001df commit 611c174
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 29 deletions.
14 changes: 14 additions & 0 deletions presets/c64/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ void raster_wait(byte line) {
while (VIC.rasterline < line) ;
}

void wait_vblank(void) {
raster_wait(250);
}

static byte VIC_BANK_PAGE[4] = {
0xc0, 0x80, 0x40, 0x00
};
Expand All @@ -24,3 +28,13 @@ char __fastcall__ poll_keyboard() {
}
#endif

void set_raster_irq(char scanline) {
// deactivate CIA interrupts (keyboard, etc)
CIA1.icr = 0x7f;
// set raster line for interrupt
VIC.ctrl1 &= 0x7f; // clear raster line bit 8
VIC.rasterline = scanline;
// activate VIC raster interrupts
VIC.imr = 1;
}

12 changes: 10 additions & 2 deletions presets/c64/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ typedef enum { false, true } bool; // boolean

// set scrolling registers
#define SET_SCROLL_Y(_y) \
VIC.ctrl1 = (VIC.ctrl1 & 0xf8) | (_y);
VIC.ctrl1 = (VIC.ctrl1 & 0xf8) | (_y & 7);

#define SET_SCROLL_X(_x) \
VIC.ctrl2 = (VIC.ctrl2 & 0xf8) | (_x);
VIC.ctrl2 = (VIC.ctrl2 & 0xf8) | (_x & 7);


// enable RAM from 0xa000-0xffff, disable interrupts
Expand All @@ -102,6 +102,9 @@ typedef enum { false, true } bool; // boolean
// wait until specific raster line
void raster_wait(byte line);

// wait until end of frame
void wait_vblank();

// get current VIC bank start address
char* get_vic_bank_start();

Expand Down Expand Up @@ -132,4 +135,9 @@ inline void waitvsync() {
}
#endif

// for use with set_irq()
// sets up the VIC to send raster interrupts
// and disables CIA interrupts
void set_raster_irq(char scanline);

#endif
4 changes: 2 additions & 2 deletions presets/c64/scroll1.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void main(void) {

// infinite loop
while (1) {
// wait for vsync
waitvsync();
// wait for end of frame
wait_vblank();
// scroll one pixel to the left
// and move screen memory every 8 pixels
scroll_one_pixel_left();
Expand Down
4 changes: 2 additions & 2 deletions presets/c64/scroll2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void scroll_one_pixel_left() {
src = scrnbuf[visbuf] + (scroll_x & 7) * 128;
// destination = hidden buffer
dst = scrnbuf[visbuf ^ 1] + (scroll_x & 7) * 128;
// wait for vsync
waitvsync();
// wait for end of frame
wait_vblank();
// scroll hidden buffer
memcpy(dst, src+1, 128);
// every 8 pixels, switch visible and hidden buffers
Expand Down
4 changes: 2 additions & 2 deletions presets/c64/scroll3.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void scroll_update_regs() {
void scroll_swap() {
// swap hidden and visible buffers
hidbuf ^= 1;
// wait for vblank and update registers
waitvsync();
// wait for end of frame and update registers
wait_vblank();
scroll_update_regs();
SET_VIC_SCREEN(hidbuf ? 0x8000 : 0x8400);
}
Expand Down
4 changes: 2 additions & 2 deletions presets/c64/scroll4.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ void main(void) {
// animate sprite in shadow sprite ram
sprite_draw(0, n++, 70, 192);
sprite_draw(0, 172, 145, 192);
// wait for vblank
waitvsync();
// wait for end of frame
wait_vblank();
// update scroll registers
// and swap screens if we must
scroll_update();
Expand Down
2 changes: 1 addition & 1 deletion presets/c64/scroll5.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void main(void) {
// animate sprite in shadow sprite ram
update_player();
// wait for end of frame
waitvsync();
wait_vblank();
// then update sprite registers
sprite_update(visbuf);
// update scroll registers
Expand Down
12 changes: 1 addition & 11 deletions presets/c64/test_setirq.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ char interrupt_handler() {
return IRQ_HANDLED;
}

void set_frame_irq(char scanline) {
// deactivate CIA interrupts (keyboard, etc)
CIA1.icr = 0x7f;
// set raster line for interrupt
VIC.ctrl1 &= 0x7f; // clear raster line bit 8
VIC.rasterline = scanline;
// activate VIC raster interrupts
VIC.imr = 1;
}

void main(void) {
clrscr();
printf("\nHello World!\n");
Expand All @@ -52,7 +42,7 @@ void main(void) {
set_irq(interrupt_handler, (void*)0x9f00, 0x100);

// disable CIA interrupt, activate VIC interrupt
set_frame_irq(255);
set_raster_irq(255);

while (1) {
printf("%d ", VIC.rasterline);
Expand Down
17 changes: 10 additions & 7 deletions src/test/testwasishim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WASIRunner } from "../common/wasi/wasishim";
import * as fs from "fs";

async function loadWASM(filename: string) {
const wasmdata = fs.readFileSync(`./wasi/${filename}.wasm`);
const wasmdata = fs.readFileSync(`./src/worker/wasm/${filename}.wasm`);
let shim = new WASIRunner();
await shim.loadAsync(wasmdata);
return shim;
Expand All @@ -12,8 +12,8 @@ async function loadWASM(filename: string) {
async function loadDASM() {
return loadWASM('dasm-wasisdk');
}
async function loadCC2600() {
return loadWASM('cc2600');
async function loadCC7800() {
return loadWASM('cc7800');
}

describe('test WASI DASM', function () {
Expand Down Expand Up @@ -66,11 +66,14 @@ describe('test WASI DASM', function () {
});
});

describe('test WASI cc2600', function () {
it('cc2600 help', async function () {
let shim = await loadCC2600();
shim.setArgs(["cc2600", '-h']);
describe('test WASI cc7800', function () {
it('cc7800 help', async function () {
let shim = await loadCC7800();
shim.setArgs(["cc7800", '-h']);
let errno = shim.run();
assert.strictEqual(errno, 0);
const stdout = shim.fds[1].getBytesAsString();
console.log(stdout);
assert.ok(stdout.indexOf('Usage: cc7800') >= 0);
});
});
Binary file added src/worker/wasm/dasm-wasisdk.wasm
Binary file not shown.

0 comments on commit 611c174

Please sign in to comment.