Skip to content

Instantly share code, notes, and snippets.

@gorkinovich
Last active June 5, 2022 19:45
Show Gist options
  • Select an option

  • Save gorkinovich/277702653e8920d0ec381bb1a5ffadf6 to your computer and use it in GitHub Desktop.

Select an option

Save gorkinovich/277702653e8920d0ec381bb1a5ffadf6 to your computer and use it in GitHub Desktop.
Test program in C language, using the CC65 compiler, to check the keyboard matrix with the Commodore 64.
#include <stdio.h>
#include <conio.h>
#define POKE(A,X) (*(unsigned char *)A) = (X)
#define PEEK(A) (*(unsigned char *)A)
#define POKEW(A,X) (*(unsigned int *)A) = (X)
#define PEEKW(A) (*(unsigned int *)A)
#define MAX_ROWS 8
#define MAX_COLS 8
void main() {
unsigned int offset;
unsigned char aux, row, col, mask[MAX_COLS], keys, color;
// Initialize the masks and the screen configuration.
aux = 1;
for(col = 0; col < MAX_COLS; ++col) {
mask[col] = aux;
aux *= 2;
}
POKE(53272, 21); // Capital Letters
POKE( 646, 1); // Cursor Color
POKE(53281, 0); // Background Color
POKE(53280, 12); // Border Color
// Clear and print the text of the program.
clrscr();
printf("keyboard test!\n");
printf("\n");
printf("del ret lrc f7 f1 f3 f5 udc\n");
printf("3 w a 4 z s e lsh\n");
printf("5 r d 6 c f t x\n");
printf("7 y g 8 b h u v\n");
printf("9 i j 0 m k o n\n");
printf("+ p l - . : @ ,\n");
printf("%c * ; hom rsh = %c /\n", 92, 94);
printf("1 %c ctr 2 spc c= q run\n", 95);
// Configure the CIA1 ports:
POKE(56322, 255); // CIA1 Port A in write mode
POKE(56323, 0); // CIA1 Port B in read mode
// Main loop of the program.
while(1) {
// First set the pointer to the first item to
// change (row number 2) in the Color RAM.
offset = 55296 + 80;
for(row = 0; row < MAX_ROWS; ++row) {
// Get the current row of the keyboard matrix.
POKE(56320, 255 - mask[row]);
keys = PEEK(56321);
for(col = 0; col < MAX_COLS; ++col) {
// If the current column is pressed, change
// the color. Otherwise use white color.
if ((keys & mask[col]) == 0) {
color = 10;
} else {
color = 1;
}
// Set the selected color in the Color RAM.
for(aux = 0; aux < 5; ++aux, ++offset) {
POKE(offset, color);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment