a500kbd

view src/main.c @ 2:a4fd9c5a6655

first working version
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 17 Oct 2017 15:25:33 +0300
parents 3228a731d4db
children 31a1f0b53d98
line source
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <avr/io.h>
4 #include <avr/interrupt.h>
5 #include <util/delay.h>
6 #include "serial.h"
7 #include "scantbl.h"
8 #include "ps2kbd.h"
9 #include "amigakb.h"
10 #include "defs.h"
11 #include "timer.h"
13 enum {
14 KF_BRK = 1,
15 KF_EXT = 2,
16 KF_EXT1 = 4,
18 KF_CTRL = 16,
19 KF_LAMIGA = 32,
20 KF_RAMIGA = 64
21 };
22 #define KF_TRANSIENT 0x0f
23 #define KF_STICKY 0xf0
25 int main(void)
26 {
27 unsigned int keyflags = 0;
28 unsigned char keycode;
29 int press;
31 /* disable all pullups globally */
32 MCUCR |= 1 << PUD;
34 DDRD = 0;
35 PORTD = 0;
36 EIMSK = 0; /* mask external interrupts */
37 EICRA = (1 << ISC11) | (1 << ISC01); /* falling edge interrupts */
39 init_timer();
41 /* initialize the UART and enable interrupts */
42 init_serial(9600);
43 sei();
45 printf("PS/2 keyboard controller - John Tsiombikas <nuclear@member.fsf.org>\r\n");
47 EIMSK = (1 << INT0) | (1 << INT1); /* enable ps/2 clock interrupt */
49 for(;;) {
50 unsigned char c = ps2read();
51 switch(c) {
52 case 0xe0: /* extended */
53 keyflags |= KF_EXT;
54 break;
56 case 0xe1: /* extended */
57 keyflags |= KF_EXT1;
58 break;
60 case 0xf0: /* break code */
61 keyflags |= KF_BRK;
62 break;
64 default:
65 press = !(keyflags & KF_BRK);
67 keycode = 0xff;
68 if(keyflags & KF_EXT) {
69 printf("ext ");
70 if(c < KEYMAP_EXT_SIZE) {
71 keycode = keymap_ext[(int)c];
72 }
73 } else if(keyflags & KF_EXT1) {
74 printf("ext1 ");
75 } else {
76 if(c < KEYMAP_NORMAL_SIZE) {
77 keycode = keymap_normal[(int)c];
78 }
79 }
81 switch(keycode) {
82 case AMIKEY_CTRL:
83 if(press)
84 keyflags |= KF_CTRL;
85 else
86 keyflags &= ~KF_CTRL;
87 break;
89 case AMIKEY_LAMI:
90 if(press)
91 keyflags |= KF_LAMIGA;
92 else
93 keyflags &= ~KF_LAMIGA;
94 break;
96 case AMIKEY_RAMI:
97 if(press)
98 keyflags |= KF_RAMIGA;
99 else
100 keyflags &= ~KF_RAMIGA;
101 break;
103 default:
104 break;
105 }
107 if((keyflags & (KF_CTRL | KF_RAMIGA | KF_LAMIGA)) == (KF_CTRL | KF_RAMIGA | KF_LAMIGA)) {
108 printf("CTRL - AMIGA - AMIGA!\r\n");
109 amikb_reset();
110 }
112 printf("scancode %x -> ", (unsigned int)c);
113 if(keycode != 0xff) {
114 amikb_sendkey(keycode, ~keyflags & KF_BRK);
115 printf("[%s] amiga key %xh\r\n", press ? "press" : "release", keycode);
116 } else {
117 printf("[%s] no translation\r\n", press ? "press" : "release");
118 }
119 keyflags &= ~KF_TRANSIENT;
120 }
121 }
122 return 0;
123 }