Skip to content

Commit f6452a7

Browse files
committed
c64: update presets, refactor analysis
1 parent de6250b commit f6452a7

File tree

14 files changed

+1101
-172
lines changed

14 files changed

+1101
-172
lines changed

presets/c64/common.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ typedef enum { false, true } bool; // boolean
2424

2525
///// MACROS /////
2626

27+
// VIC Control Register 1 Flags
28+
#define VIC_CTRL1_RST8 0x80 // Bit 8 of RASTER (read) or raster line interrupt set (write)
29+
#define VIC_CTRL1_ECM 0x40 // Extended Color Mode
30+
#define VIC_CTRL1_BMM 0x20 // Bitmap Mode
31+
#define VIC_CTRL1_DEN 0x10 // Display Enable
32+
#define VIC_CTRL1_RSEL 0x08 // Row Select (25 or 24 rows)
33+
#define VIC_CTRL1_YSCROLL_MASK 0x07 // Vertical Fine Scrolling
34+
35+
// VIC Control Register 2 Flags
36+
#define VIC_CTRL2_RES 0x20 // Chip reset
37+
#define VIC_CTRL2_MCM 0x10 // Multicolor Mode Enable
38+
#define VIC_CTRL2_CSEL 0x08 // Column Select (40 or 38 columns)
39+
#define VIC_CTRL2_XSCROLL_MASK 0x07 // Horizontal Fine Scrolling
40+
41+
// VIC Memory Control Register Flags
42+
#define VIC_ADDR_VM_MASK 0xf0 // Video Matrix Base Address Mask (character data)
43+
#define VIC_ADDR_CB_MASK 0x0e // Character Bank Base Address Mask (screen memory)
44+
45+
// VIC Interrupt Register Flags
46+
#define VIC_IRR_IRQ 0x80 // Interrupt Request
47+
#define VIC_IRR_ILP 0x08 // Light Pen Interrupt
48+
#define VIC_IRR_IMMC 0x04 // Sprite-Sprite Collision Interrupt
49+
#define VIC_IRR_IMBC 0x02 // Sprite-Background Collision Interrupt
50+
#define VIC_IRR_IRST 0x01 // Raster Line Interrupt
51+
52+
// VIC Interrupt Mask Register Flags
53+
#define VIC_IMR_ELP 0x08 // Enable Light Pen Interrupt
54+
#define VIC_IMR_EMMC 0x04 // Enable Sprite-Sprite Collision Interrupt
55+
#define VIC_IMR_EMBC 0x02 // Enable Sprite-Background Collision Interrupt
56+
#define VIC_IMR_ERST 0x01 // Enable Raster Interrupt
57+
2758
// lookup screen address macro
2859
#define SCRNADR(base,col,row) ((base)+(col)+(row)*40)
2960

@@ -77,6 +108,16 @@ char* get_vic_bank_start();
77108
// get current screen memory address
78109
char* get_screen_memory();
79110

111+
// read joystick fast
112+
#define READ_STICK(index) ~PEEK(0xdc01-(index))
113+
114+
#define STICK_UP(joy) ((joy & 0x1) != 0)
115+
#define STICK_DOWN(joy) ((joy & 0x2) != 0)
116+
#define STICK_LEFT(joy) ((joy & 0x4) != 0)
117+
#define STICK_RIGHT(joy) ((joy & 0x8) != 0)
118+
#define STICK_BUTTON(joy) ((joy & 0x10) != 0)
119+
#define STICK_MOVED(joy) ((joy & 0x1f) != 0)
120+
80121
#ifdef __CC65__
81122
// return key in buffer, or 0 if none (BIOS call)
82123
char __fastcall__ poll_keyboard();

presets/c64/plasma.c

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/*****************************************************************************\
2+
** plasma test program for cc65. **
3+
** **
4+
** (w)2001 by groepaz **
5+
** **
6+
** Cleanup and porting by Ullrich von Bassewitz. **
7+
** **
8+
\*****************************************************************************/
9+
10+
11+
12+
#include <stdlib.h>
13+
#include <time.h>
14+
#include <conio.h>
15+
#include <cc65.h>
16+
17+
18+
19+
#if defined(__C64__) || defined(__C128__)
20+
# define SCREEN1 0xE000
21+
# define SCREEN2 0xE400
22+
# define CHARSET 0xE800
23+
# define outb(addr,val) (*(addr)) = (val)
24+
# define inb(addr) (*(addr))
25+
#elif defined(__CBM510__)
26+
# define SCREEN1 0xF000
27+
# define SCREEN2 0xF400
28+
# define CHARSET 0xE000
29+
# define outb(addr,val) pokebsys ((unsigned)(addr), val)
30+
# define inb(addr) peekbsys ((unsigned)(addr))
31+
#elif defined(__PLUS4__)
32+
# define SCREEN1 0x6400
33+
# define SCREEN2 0x6C00
34+
# define CHARSET 0x7000
35+
# define outb(addr,val) (*(addr)) = (val)
36+
# define inb(addr) (*(addr))
37+
#endif
38+
39+
40+
41+
/* Values for the VIC address register to switch between the two pages */
42+
#if defined(__PLUS4__)
43+
#define PAGE1 ((SCREEN1 >> 8) & 0xF8)
44+
#define PAGE2 ((SCREEN2 >> 8) & 0xF8)
45+
#define CHARADR ((CHARSET >> 8) & 0xFC)
46+
#else
47+
#define PAGE1 ((SCREEN1 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
48+
#define PAGE2 ((SCREEN2 >> 6) & 0xF0) | ((CHARSET >> 10) & 0x0E)
49+
#endif
50+
51+
52+
53+
/* Use static local variables for speed */
54+
#pragma static-locals (1);
55+
56+
57+
static const unsigned char sinustable[0x100] = {
58+
0x80, 0x7d, 0x7a, 0x77, 0x74, 0x70, 0x6d, 0x6a,
59+
0x67, 0x64, 0x61, 0x5e, 0x5b, 0x58, 0x55, 0x52,
60+
0x4f, 0x4d, 0x4a, 0x47, 0x44, 0x41, 0x3f, 0x3c,
61+
0x39, 0x37, 0x34, 0x32, 0x2f, 0x2d, 0x2b, 0x28,
62+
0x26, 0x24, 0x22, 0x20, 0x1e, 0x1c, 0x1a, 0x18,
63+
0x16, 0x15, 0x13, 0x11, 0x10, 0x0f, 0x0d, 0x0c,
64+
0x0b, 0x0a, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04,
65+
0x03, 0x03, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
66+
0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x03,
67+
0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0a,
68+
0x0b, 0x0c, 0x0d, 0x0f, 0x10, 0x11, 0x13, 0x15,
69+
0x16, 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24,
70+
0x26, 0x28, 0x2b, 0x2d, 0x2f, 0x32, 0x34, 0x37,
71+
0x39, 0x3c, 0x3f, 0x41, 0x44, 0x47, 0x4a, 0x4d,
72+
0x4f, 0x52, 0x55, 0x58, 0x5b, 0x5e, 0x61, 0x64,
73+
0x67, 0x6a, 0x6d, 0x70, 0x74, 0x77, 0x7a, 0x7d,
74+
0x80, 0x83, 0x86, 0x89, 0x8c, 0x90, 0x93, 0x96,
75+
0x99, 0x9c, 0x9f, 0xa2, 0xa5, 0xa8, 0xab, 0xae,
76+
0xb1, 0xb3, 0xb6, 0xb9, 0xbc, 0xbf, 0xc1, 0xc4,
77+
0xc7, 0xc9, 0xcc, 0xce, 0xd1, 0xd3, 0xd5, 0xd8,
78+
0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8,
79+
0xea, 0xeb, 0xed, 0xef, 0xf0, 0xf1, 0xf3, 0xf4,
80+
0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfa, 0xfb, 0xfc,
81+
0xfd, 0xfd, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff,
82+
0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfd,
83+
0xfd, 0xfc, 0xfb, 0xfa, 0xfa, 0xf9, 0xf8, 0xf6,
84+
0xf5, 0xf4, 0xf3, 0xf1, 0xf0, 0xef, 0xed, 0xeb,
85+
0xea, 0xe8, 0xe6, 0xe4, 0xe2, 0xe0, 0xde, 0xdc,
86+
0xda, 0xd8, 0xd5, 0xd3, 0xd1, 0xce, 0xcc, 0xc9,
87+
0xc7, 0xc4, 0xc1, 0xbf, 0xbc, 0xb9, 0xb6, 0xb3,
88+
0xb1, 0xae, 0xab, 0xa8, 0xa5, 0xa2, 0x9f, 0x9c,
89+
0x99, 0x96, 0x93, 0x90, 0x8c, 0x89, 0x86, 0x83
90+
};
91+
92+
93+
94+
static void doplasma (register unsigned char* scrn)
95+
{
96+
unsigned char xbuf[40];
97+
unsigned char ybuf[25];
98+
unsigned char c1a,c1b;
99+
unsigned char c2a,c2b;
100+
unsigned char c1A,c1B;
101+
unsigned char c2A,c2B;
102+
register unsigned char i, ii;
103+
104+
c1a = c1A;
105+
c1b = c1B;
106+
for (ii = 0; ii < 25; ++ii) {
107+
ybuf[ii] = (sinustable[c1a] + sinustable[c1b]);
108+
c1a += 4;
109+
c1b += 9;
110+
}
111+
c1A += 3;
112+
c1B -= 5;
113+
c2a = c2A;
114+
c2b = c2B;
115+
for (i = 0; i < 40; ++i) {
116+
xbuf[i] = (sinustable[c2a] + sinustable[c2b]);
117+
c2a += 3;
118+
c2b += 7;
119+
}
120+
c2A += 2;
121+
c2B -= 3;
122+
for (ii = 0; ii < 25; ++ii) {
123+
/* Unrolling the following loop will give a speed increase of
124+
** nearly 100% (~24fps), but it will also increase the code
125+
** size a lot.
126+
*/
127+
for (i = 0; i < 40; ++i, ++scrn) {
128+
*scrn = (xbuf[i] + ybuf[ii]);
129+
}
130+
}
131+
}
132+
133+
static void makechar (void)
134+
{
135+
static const unsigned char bittab[8] = {
136+
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
137+
};
138+
unsigned char i, ii, b, s;
139+
unsigned c;
140+
141+
gotoxy (0, 1);
142+
for (c = 0; c < 0x100; ++c) {
143+
s = sinustable[c];
144+
for (i = 0; i < 8; ++i){
145+
b = 0;
146+
for (ii = 0; ii < 8; ++ii) {
147+
if ((rand() & 0xFFu) > s) {
148+
b |= bittab[ii];
149+
}
150+
}
151+
((unsigned char*)CHARSET) [(c*8) + i] = b;
152+
}
153+
if ((c & 0x07) == 0) {
154+
cputc ('.');
155+
}
156+
}
157+
}
158+
159+
160+
161+
int main (void)
162+
{
163+
unsigned char border;
164+
unsigned char background;
165+
unsigned char text;
166+
unsigned char v;
167+
clock_t t;
168+
unsigned long f = 0;
169+
unsigned long sec;
170+
unsigned sec10;
171+
unsigned long fps;
172+
unsigned fps10;
173+
174+
175+
#if defined(__C64__)
176+
unsigned char block;
177+
#endif
178+
#if defined(__C128__)
179+
unsigned char block;
180+
unsigned char initflag;
181+
unsigned char graphflag;
182+
#endif
183+
#if defined(__PLUS4__)
184+
unsigned int i;
185+
unsigned char v2;
186+
#endif
187+
188+
clrscr ();
189+
cprintf ("Making charset, mompls");
190+
makechar();
191+
192+
/* Set the border and background colors */
193+
border = bordercolor (COLOR_BLUE);
194+
background = bgcolor (COLOR_BLUE);
195+
text = textcolor (COLOR_BLACK);
196+
clrscr ();
197+
198+
#if defined(__C64__) || defined(__C128__)
199+
/* Move the VIC 16K block */
200+
block = inb (&CIA2.pra);
201+
outb (&CIA2.pra, (block & 0xFC) | ((SCREEN1 >> 14) ^ 0x03));
202+
#endif
203+
#if defined(__C128__)
204+
/* Save and change some flags, so that kernal/basic interrupt handler will
205+
** not interfere with our routine.
206+
*/
207+
initflag = *(unsigned char*) 0xA04;
208+
*(unsigned char*) 0xA04 &= 0xFE;
209+
graphflag = *(unsigned char*) 0xD8;
210+
*(unsigned char*) 0xD8 = 0xFF;
211+
#endif
212+
213+
/* Remember the VIC address register */
214+
#if defined(__PLUS4__)
215+
v = inb (&TED.char_addr);
216+
v2 = inb (&TED.video_addr);
217+
#else
218+
v = inb (&VIC.addr);
219+
#endif
220+
221+
#if defined(__PLUS4__)
222+
for (i=0;i<1000;i++) {
223+
((unsigned char *) (SCREEN1-0x0400))[i] = 0;
224+
((unsigned char *) (SCREEN2-0x0400))[i] = 0;
225+
}
226+
outb (&TED.char_addr, CHARADR);
227+
#endif
228+
229+
/* Run the demo until a key was hit */
230+
t = clock ();
231+
while (!kbhit()) {
232+
/* Build page 1, then make it visible */
233+
doplasma ((unsigned char*)SCREEN1);
234+
#if defined(__PLUS4__)
235+
outb (&TED.video_addr, PAGE1);
236+
#else
237+
outb (&VIC.addr, PAGE1);
238+
#endif
239+
240+
/* Build page 2, then make it visible */
241+
doplasma ((unsigned char*)SCREEN2);
242+
#if defined(__PLUS4__)
243+
outb (&TED.video_addr, PAGE2);
244+
#else
245+
outb (&VIC.addr, PAGE2);
246+
#endif
247+
248+
/* Count frames */
249+
f += 2;
250+
}
251+
t = clock() - t;
252+
253+
/* Switch back the VIC screen */
254+
#if defined(__PLUS4__)
255+
outb (&TED.video_addr, v2);
256+
outb (&TED.char_addr, v);
257+
#else
258+
outb (&VIC.addr, v);
259+
#endif
260+
261+
#if defined(__C64__) || defined(__C128__)
262+
/* Move back the VIC 16K block */
263+
outb (&CIA2.pra, block);
264+
#endif
265+
#if defined(__C128__)
266+
/* Restore the flags */
267+
*(unsigned char*) 0xA04 = initflag;
268+
*(unsigned char*) 0xD8 = graphflag;
269+
#endif
270+
271+
/* Fetch the character from the keyboard buffer and discard it */
272+
(void) cgetc();
273+
274+
/* Reset screen colors */
275+
bordercolor (border);
276+
bgcolor (background);
277+
textcolor (text);
278+
clrscr ();
279+
280+
/* Calculate stats */
281+
sec = (t * 10) / CLK_TCK;
282+
sec10 = sec % 10;
283+
sec /= 10;
284+
fps = (f * (CLK_TCK * 10)) / t;
285+
fps10 = fps % 10;
286+
fps /= 10;
287+
288+
/* Output stats */
289+
gotoxy (0, 0); cprintf ("time : %lu.%us", sec, sec10);
290+
gotoxy (0, 1); cprintf ("frames: %lu", f);
291+
gotoxy (0, 2); cprintf ("fps : %lu.%u", fps, fps10);
292+
293+
if (doesclrscrafterexit ()) {
294+
cputsxy (0, 4, "Press any key when done...");
295+
(void) cgetc ();
296+
}
297+
298+
/* Done */
299+
return EXIT_SUCCESS;
300+
}

0 commit comments

Comments
 (0)