Skip to content

Commit 8db8bbd

Browse files
committed
Added simple keypad and interrupt handling
1 parent 935cda7 commit 8db8bbd

File tree

9 files changed

+127
-6
lines changed

9 files changed

+127
-6
lines changed

samples/int_test/int_test.asm

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
ROM_START .equ $8000
2+
3+
RESET_VECTOR .equ $fffc
4+
5+
.org ROM_START
6+
7+
data_display:
8+
ldx #$00 ; Initialize counter
9+
loop_data:
10+
lda data,x ; Load data bytes from address data + x
11+
beq end_prog ; On end of stream move to end of program
12+
jsr write_data
13+
inx ; Increase counter
14+
jmp loop_data
15+
end_prog:
16+
cli
17+
loop:
18+
jmp loop
19+
20+
write_data:
21+
jsr pulse ; Pulse 6522
22+
rts ; Return
23+
24+
pulse:
25+
sta $ff00
26+
rts ; Return from subroutine
27+
28+
data:
29+
string "Merry Christmas!"
30+
31+
interrupt:
32+
pha
33+
lda #$42
34+
pla
35+
rti
36+
37+
.org RESET_VECTOR
38+
word data_display
39+
word interrupt
40+

samples/int_test/make.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vasm6502_oldstyle -dotdir -wdc02 -Fbin int_test.asm -L int_test.lst -o int_test.bin
2+
hexdump -C int_test.bin

samples/int_test/test.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
x6502 int_test.bin

src/cpu.c

+2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ cpu * new_cpu() {
1616
memset(m->mem, 0xFF, MEMORY_SIZE);
1717
m->v1 = new_via();
1818
m->l = new_lcd();
19+
m->k = new_keys();
1920
return m;
2021
}
2122

2223
void destroy_cpu(cpu* m) {
2324
destroy_via(m->v1);
2425
destroy_lcd(m->l);
26+
destroy_keys(m->k);
2527
free(m);
2628
}

src/cpu.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdbool.h>
66
#include "via.h"
77
#include "lcd.h"
8+
#include "keys.h"
89

910
#define MEMORY_SIZE 65536
1011
#define STACK_START 0x0100
@@ -58,8 +59,8 @@ typedef struct {
5859
via* v1;
5960
// LCD
6061
lcd* l;
61-
// noninteractive mode
62-
bool noninteractive;
62+
// keys
63+
keys* k;
6364
} cpu;
6465

6566
cpu * new_cpu();

src/gui.c

+26-4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ void update_gui(cpu *m) {
215215
} else {
216216
input_cycle_skip=0;
217217

218+
m->k->key_up=false;
219+
m->k->key_down=false;
220+
m->k->key_left=false;
221+
m->k->key_right=false;
222+
m->k->key_enter=false;
223+
218224
switch (m->clock_mode) {
219225
case CLOCK_SPRINT:
220226
case CLOCK_FAST:
@@ -253,6 +259,26 @@ void update_gui(cpu *m) {
253259
m->clock_mode = CLOCK_FAST;
254260
}
255261
break;
262+
case 10:
263+
m->k->key_enter = true;
264+
keep_going = true;
265+
break;
266+
case KEY_UP:
267+
m->k->key_up = true;
268+
keep_going = true;
269+
break;
270+
case KEY_DOWN:
271+
m->k->key_down = true;
272+
keep_going = true;
273+
break;
274+
case KEY_LEFT:
275+
m->k->key_left = true;
276+
keep_going = true;
277+
break;
278+
case KEY_RIGHT:
279+
m->k->key_right = true;
280+
keep_going = true;
281+
break;
256282
case '[':
257283
if (memory_start > 0x00) {
258284
memory_start--;
@@ -277,10 +303,6 @@ void update_gui(cpu *m) {
277303
memory_start = 0xfe;
278304
}
279305
break;
280-
default:
281-
m->interrupt_waiting = 0x01;
282-
m->mem[IO_GETCHAR] = read;
283-
keep_going = true;
284306
}
285307
}
286308
} while (!keep_going && m->clock_mode != CLOCK_SPRINT);

src/io.c

+12
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,21 @@ void handle_io(cpu *m, bool rwb) {
4545
}
4646
}
4747
} else {
48+
uint8_t old_porta_input = m->v1->porta & ~m->v1->ddra;
49+
4850
m->v1->portb &= m->v1->ddrb;
4951
m->v1->portb |= (m->l->data & ~m->v1->ddrb);
5052
m->v1->porta &= m->v1->ddra;
53+
m->v1->porta |= m->k->key_enter ? 0x01 : 0;
54+
m->v1->porta |= m->k->key_up ? 0x02 : 0;
55+
m->v1->porta |= m->k->key_down ? 0x04 : 0;
56+
m->v1->porta |= m->k->key_left ? 0x08 : 0;
57+
m->v1->porta |= m->k->key_right ? 0x10 : 0;
58+
59+
if (old_porta_input != (m->v1->porta & ~m->v1->ddra)) {
60+
m->interrupt_waiting = 0x01;
61+
}
62+
5163
// read operation
5264
m->mem[0x6000] = m->v1->portb;
5365
m->mem[0x6001] = m->v1->porta;

src/keys.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "keys.h"
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
5+
keys * new_keys() {
6+
keys *k = malloc(sizeof(keys));
7+
8+
k->key_up = false;
9+
k->key_down = false;
10+
k->key_left = false;
11+
k->key_right = false;
12+
k->key_enter = false;
13+
14+
return k;
15+
}
16+
17+
void destroy_keys(keys* k) {
18+
free(k);
19+
}
20+

src/keys.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __6502_KEYS__
2+
#define __6502_KEYS__
3+
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
7+
typedef struct {
8+
// key states
9+
bool key_up;
10+
bool key_down;
11+
bool key_left;
12+
bool key_right;
13+
bool key_enter;
14+
} keys;
15+
16+
keys* new_keys();
17+
18+
void destroy_keys(keys* l);
19+
20+
#endif

0 commit comments

Comments
 (0)