1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-23 03:29:39 +00:00

added aspect: ratio to assets, c64 clears kbd on reset

This commit is contained in:
Steven Hugg 2020-01-31 17:45:16 -06:00
parent 03af8c2714
commit 411795f8fa
7 changed files with 453 additions and 4 deletions

13
presets/c64/bcd.c Normal file
View File

@ -0,0 +1,13 @@
#include "neslib.h"
word bcd_add(word a, word b) {
register word c, d; // intermediate values
c = a + 0x0666; // add 6 to each BCD digit
d = c ^ b; // sum without carry propagation
c += b; // provisional sum
d = ~(c ^ d) & 0x1110; // just the BCD carry bits
d = (d >> 2) | (d >> 3); // correction
return c - d; // corrected BCD sum
}

48
presets/c64/scroll1.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <conio.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <string.h>
#include <stdlib.h>
typedef unsigned char byte;
typedef unsigned short word;
void rasterWait(unsigned char line) {
while (VIC.rasterline < line) ;
}
byte x = 0; // x scroll position
byte y = 0; // y scroll position
byte* scrnbuf; // screen buffer
void main(void) {
clrscr();
printf("\r\n Hello World!");
printf("\r\n\r\n This is how we scroll");
printf("\r\n\r\n One line at a time");
printf("\r\n\r\n But we don't have time");
printf("\r\n\r\n To copy all the bytes ");
VIC.ctrl1 = 0x10; // 24 lines
VIC.ctrl2 = 0x00; // 38 columns
// get screen buffer address
scrnbuf = (byte*)((VIC.addr << 6) & 0x3c00);
// infinite loop
while (1) {
x--;
// set scroll registers
VIC.ctrl1 = VIC.ctrl1 & 0xf8;
VIC.ctrl1 |= (y & 7);
VIC.ctrl2 = VIC.ctrl2 & 0xf8;
VIC.ctrl2 |= (x & 7);
// wait for vsync
rasterWait(255);
// every 8 pixels, move screen cells
if ((x & 7) == 0) {
memcpy(scrnbuf, scrnbuf+1, 40*8-1);
}
}
}

61
presets/c64/scroll2.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <conio.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <string.h>
#include <stdlib.h>
typedef unsigned char byte;
typedef unsigned short word;
void rasterWait(unsigned char line) {
while (VIC.rasterline < line) ;
}
byte x = 0; // x scroll position
byte y = 0; // y scroll position
byte* scrnbuf[2]; // screen buffer(s)
byte frame = 0;
void main(void) {
byte* src;
byte* dst;
clrscr();
printf("\r\n\r\n\r\n Hello World!");
printf("\r\n\r\n\r\n This is how we scroll");
printf("\r\n\r\n\r\n One line at a time");
printf("\r\n\r\n\r\n And now we have two buffers");
printf("\r\n\r\n\r\n To copy all the bytes ");
VIC.ctrl1 = 0x10; // 24 lines
VIC.ctrl2 = 0x00; // 38 columns
// get screen buffer addresses
scrnbuf[0] = (byte*) 0x400;
scrnbuf[1] = (byte*) 0x3c00;
memcpy(scrnbuf[1], scrnbuf[0], 24*40);
// infinite loop
while (1) {
// scroll left
x--;
// set scroll registers
VIC.ctrl1 = VIC.ctrl1 & 0xf8;
VIC.ctrl1 |= (y & 7);
VIC.ctrl2 = VIC.ctrl2 & 0xf8;
VIC.ctrl2 |= (x & 7);
// calculate frame pointers
src = scrnbuf[frame&1] + (x&7)*128;
dst = scrnbuf[frame&1^1] + (x&7)*128;
// wait for vsync
rasterWait(255);
// scroll hidden buffer
memcpy(dst, src+1, 128);
// every 8 pixels, switch farmes
if ((x & 7) == 0) {
VIC.addr = (VIC.addr & 0xf) | ((frame & 1) ? 0x10 : 0xf0);
frame++;
}
}
}

135
presets/c64/scroll3.c Normal file
View File

@ -0,0 +1,135 @@
#include <stdio.h>
#include <conio.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <joystick.h>
typedef uint8_t byte;
typedef uint16_t word;
typedef int8_t sbyte;
#define COLS 40
#define ROWS 25
void raster_wait(unsigned char line) {
while (VIC.rasterline < line) ;
}
void wait_vblank() {
raster_wait(255); // TODO
}
sbyte scroll_fine_x = 0;
sbyte scroll_fine_y = 0;
byte curbuf = 0;
byte* scrnbuf[2]; // screen buffer(s)
void scroll_update_regs() {
VIC.ctrl1 = (VIC.ctrl1 & 0xf8) | scroll_fine_y;
VIC.ctrl2 = (VIC.ctrl2 & 0xf8) | scroll_fine_x;
}
void scroll_swap() {
// swap hidden and visible buffers
curbuf ^= 1;
// wait for vblank and update registers
wait_vblank();
scroll_update_regs();
VIC.addr = (VIC.addr & 0xf) | (curbuf ? 0x00 : 0x10);
// copy visible buffer to hidden buffer
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1], COLS*ROWS);
}
void scroll_left() {
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1]+1, COLS*ROWS-1);
scroll_swap();
}
void scroll_right() {
memcpy(scrnbuf[curbuf]+1, scrnbuf[curbuf^1], COLS*ROWS-1);
scroll_swap();
}
void scroll_up() {
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1]+COLS, COLS*(ROWS-1));
scroll_swap();
}
void scroll_down() {
memcpy(scrnbuf[curbuf]+COLS, scrnbuf[curbuf^1], COLS*(ROWS-1));
scroll_swap();
}
void scroll_horiz(sbyte delta_x) {
scroll_fine_x += delta_x;
while (scroll_fine_x < 0) {
scroll_fine_x += 8;
scroll_left();
}
while (scroll_fine_x >= 8) {
scroll_fine_x -= 8;
scroll_right();
}
}
void scroll_vert(sbyte delta_y) {
scroll_fine_y += delta_y;
while (scroll_fine_y < 0) {
scroll_fine_y += 8;
scroll_up();
}
while (scroll_fine_y >= 8) {
scroll_fine_y -= 8;
scroll_down();
}
}
void scroll_setup() {
// get screen buffer addresses
scrnbuf[0] = (byte*) 0x8000;
scrnbuf[1] = (byte*) 0x8400;
// copy existing text to screen 0
memcpy(scrnbuf[0], (byte*)0x400, COLS*ROWS);
// copy screen 1 to screen 0
memcpy(scrnbuf[1], scrnbuf[0], COLS*ROWS);
// set VIC bank ($4000-$7FFF)
// https://www.c64-wiki.com/wiki/VIC_bank
CIA2.pra = 0x01;
VIC.ctrl1 = 0x10; // 24 lines
VIC.ctrl2 = 0x00; // 38 columns
}
void main(void) {
clrscr();
printf("\r\n\r\n\r\n Hello World!");
printf("\r\n\r\n\r\n This is how we scroll");
printf("\r\n\r\n\r\n One line at a time");
printf("\r\n\r\n\r\n And now we have two buffers");
printf("\r\n\r\n\r\n To copy all the bytes ");
scroll_setup();
// install the joystick driver
joy_install (joy_static_stddrv);
// infinite loop
while (1) {
// get joystick bits
char joy = joy_read(0);
// move sprite based on arrow keys
if (JOY_LEFT(joy)) scroll_horiz(-1);
if (JOY_UP(joy)) scroll_vert(-1);
if (JOY_RIGHT(joy)) scroll_horiz(1);
if (JOY_DOWN(joy)) scroll_vert(1);
// update regs
wait_vblank();
scroll_update_regs();
}
}

185
presets/c64/scroll4.c Normal file
View File

@ -0,0 +1,185 @@
#include <stdio.h>
#include <conio.h>
#include <c64.h>
#include <cbm_petscii_charmap.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <joystick.h>
typedef uint8_t byte;
typedef uint16_t word;
typedef int8_t sbyte;
#define COLS 40
#define ROWS 25
void raster_wait(unsigned char line) {
while (VIC.rasterline < line) ;
}
void wait_vblank() {
raster_wait(255); // TODO
}
sbyte scroll_fine_x = 0;
sbyte scroll_fine_y = 0;
byte origin_x = 0x80;
byte origin_y = 0x80;
byte curbuf = 0;
byte* scrnbuf[2]; // screen buffer(s)
byte tempbuf[COLS*ROWS];
void draw_cell(byte x, byte y) {
byte xx = x + origin_x;
byte yy = y + origin_y;
byte ch = xx ^ yy;
word ofs = x+y*COLS;
scrnbuf[curbuf][ofs] = ch; // character
tempbuf[ofs] = ch; // color
}
void scroll_draw_column(byte col) {
byte y;
for (y=0; y<ROWS; y++) {
draw_cell(col, y);
}
}
void scroll_draw_row(byte row) {
byte x;
for (x=0; x<COLS; x++) {
draw_cell(x, row);
}
}
void scroll_update_regs() {
VIC.ctrl1 = (VIC.ctrl1 & 0xf8) | scroll_fine_y;
VIC.ctrl2 = (VIC.ctrl2 & 0xf8) | scroll_fine_x;
}
void scroll_swap() {
// swap hidden and visible buffers
curbuf ^= 1;
// wait for vblank and update registers
wait_vblank();
scroll_update_regs();
VIC.addr = (VIC.addr & 0xf) | (curbuf ? 0x00 : 0x10);
}
void scroll_copy() {
// copy temp buf to color ram
memcpy(COLOR_RAM, tempbuf, COLS*ROWS);
// copy visible buffer to hidden buffer
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1], COLS*ROWS);
}
// TODO: left and up can be faster, b/c we can copy color ram downward
void scroll_left() {
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1]+1, COLS*ROWS-1);
++origin_x;
memcpy(tempbuf, COLOR_RAM+1, COLS*ROWS-1);
scroll_draw_column(COLS-1);
scroll_swap();
scroll_copy();
}
void scroll_up() {
memcpy(scrnbuf[curbuf], scrnbuf[curbuf^1]+COLS, COLS*(ROWS-1));
++origin_y;
memcpy(tempbuf, COLOR_RAM+COLS, COLS*(ROWS-1));
scroll_draw_row(ROWS-1);
scroll_swap();
scroll_copy();
}
void scroll_right() {
memcpy(scrnbuf[curbuf]+1, scrnbuf[curbuf^1], COLS*ROWS-1);
--origin_x;
memcpy(tempbuf+1, COLOR_RAM, COLS*ROWS-1);
scroll_draw_column(0);
scroll_swap();
scroll_copy();
}
void scroll_down() {
memcpy(scrnbuf[curbuf]+COLS, scrnbuf[curbuf^1], COLS*(ROWS-1));
--origin_y;
memcpy(tempbuf+COLS, COLOR_RAM, COLS*(ROWS-1));
scroll_draw_row(0);
scroll_swap();
scroll_copy();
}
void scroll_horiz(sbyte delta_x) {
scroll_fine_x += delta_x;
while (scroll_fine_x < 0) {
scroll_fine_x += 8;
scroll_left();
}
while (scroll_fine_x >= 8) {
scroll_fine_x -= 8;
scroll_right();
}
}
void scroll_vert(sbyte delta_y) {
scroll_fine_y += delta_y;
while (scroll_fine_y < 0) {
scroll_fine_y += 8;
scroll_up();
}
while (scroll_fine_y >= 8) {
scroll_fine_y -= 8;
scroll_down();
}
}
void scroll_setup() {
// get screen buffer addresses
scrnbuf[0] = (byte*) 0x8000;
scrnbuf[1] = (byte*) 0x8400;
// copy existing text to screen 0
memcpy(scrnbuf[0], (byte*)0x400, COLS*ROWS);
// copy screen 1 to screen 0
memcpy(scrnbuf[1], scrnbuf[0], COLS*ROWS);
// set VIC bank ($4000-$7FFF)
// https://www.c64-wiki.com/wiki/VIC_bank
CIA2.pra = 0x01;
VIC.ctrl1 = 0x10; // 24 lines
VIC.ctrl2 = 0x00; // 38 columns
}
void main(void) {
sbyte n =0;
clrscr();
printf("\r\n\r\n\r\n Hello World!");
printf("\r\n\r\n\r\n This is how we scroll");
printf("\r\n\r\n\r\n But color RAM can't move");
printf("\r\n\r\n\r\n So we have to use a temp buffer");
printf("\r\n\r\n\r\n And copy it just in time");
scroll_setup();
// install the joystick driver
joy_install (joy_static_stddrv);
// infinite loop
while (1) {
// get joystick bits
char joy = joy_read(0);
// move sprite based on arrow keys
if (JOY_LEFT(joy)) scroll_horiz(-1);
if (JOY_UP(joy)) scroll_vert(-1);
if (JOY_RIGHT(joy)) scroll_horiz(1);
if (JOY_DOWN(joy)) scroll_vert(1);
// update regs
wait_vblank();
scroll_update_regs();
}
}

View File

@ -41,6 +41,7 @@ export type PixelEditorImageFormat = {
destfmt?:PixelEditorImageFormat
xform?:string
skip?:number
aspect?:number
};
export type PixelEditorPaletteFormat = {
@ -818,8 +819,9 @@ export class CharmapEditor extends PixNode {
chooser.width = this.fmt.w || 1;
chooser.height = this.fmt.h || 1;
chooser.recreate(agrid, (index, viewer) => {
var escale = Math.ceil(192 / this.fmt.w);
var editview = this.createEditor(aeditor, viewer, escale);
var yscale = Math.ceil(192 / this.fmt.w);
var xscale = yscale * (this.fmt.aspect || 1.0);
var editview = this.createEditor(aeditor, viewer, xscale, yscale);
this.context.setCurrentEditor(aeditor, $(viewer.canvas), this);
this.rgbimgs[index] = viewer.rgbdata;
});
@ -843,11 +845,12 @@ export class CharmapEditor extends PixNode {
return true;
}
createEditor(aeditor : JQuery, viewer : Viewer, escale : number) : PixEditor {
createEditor(aeditor: JQuery, viewer: Viewer, xscale: number, yscale: number) : PixEditor {
var im = new PixEditor();
im.createWith(viewer);
im.updateImage();
im.canvas.style.width = (viewer.width*escale)+'px'; // TODO
im.canvas.style.width = (viewer.width*xscale)+'px'; // TODO
im.canvas.style.height = (viewer.height*yscale)+'px'; // TODO
im.makeEditable(this, aeditor, this.left.palette);
return im;
}

View File

@ -587,6 +587,10 @@ export class C64_WASMMachine extends BaseWASMMachine implements Machine {
this.initstring = "\r\r\r\r\r\r\r\r\r\r\rSYS " + this.prgstart + "\r";
this.initindex = 0;
}
// clear keyboard
for (var ch=0; ch<128; ch++) {
this.setKeyInput(ch, 0, KeyFlags.KeyUp);
}
}
advanceFrame(trap: TrapCondition) : number {
this.typeInitString(); // type init string into console (TODO: doesnt work during debug)