commit 0fe6aa665dd52fdc1780e148fa9fe6240c39e710
parent d04d4156875db61ec974555347523d3a3dc0d3cb
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 29 Jul 2022 07:28:16 -0400
Add PS/2 subsystem
Diffstat:
6 files changed, 82 insertions(+), 25 deletions(-)
diff --git a/fs/drv/pc/pci.fs b/fs/drv/pc/pci.fs
@@ -0,0 +1,10 @@
+\ PCI driver
+
+$cf8 const CFGADDR
+$cfc const CFGDATA
+
+: pciaddr ( bus slot func off -- n )
+ $fc and swap 8 lshift or swap 11 lshift or swap 16 lshift or $80000000 or ;
+
+: pci@ ( bus slot func off -- n )
+ pciaddr CFGADDR p! CFGDATA p@ ;
diff --git a/fs/drv/pc/ps28042.fs b/fs/drv/pc/ps28042.fs
@@ -0,0 +1,6 @@
+\ 8042 PS/2 Controller driver
+$60 const PS2DATA
+$64 const PS2CMD
+
+: 8042ps2@? ( -- keycode? f )
+ PS2CMD pc@ 1 and dup if PS2DATA pc@ swap then ;
diff --git a/fs/sys/ps2.fs b/fs/sys/ps2.fs
@@ -0,0 +1,60 @@
+\ PS/2 subsystem
+\ For now, hardcoded to PS/2 Set 1 because that's what QEMU and my first test
+\ hardware has.
+
+\ b0=lshift b1=rshift b2=lctrl b3=rctrl b4=lalt b5=ralt b6=lgui b7=rgui
+\ b8=capslock b9=numlock b10=scrolllock
+0 value _flags
+: _shift? _flags 3 and ;
+
+\ Return a PS/2 keycode if one is available.
+alias abort ps2@? ( -- keycode? f )
+
+\ Table mapping Make codes of Set 1 to ASCII char
+\ $80-$8f values are flags
+\ $90-$9f values are F keys
+
+\ 0 1 2 3 4 5 6 7 8 9 a b c d e f
+create _maplower $80 nc,
+0 0 '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' '-' '=' BS $09
+'q' 'w' 'e' 'r' 't' 'y' 'u' 'i' 'o' 'p' '[' ']' CR $82 'a' 's'
+'d' 'f' 'g' 'h' 'j' 'k' 'l' ';' ''' '`' $80 '\' 'z' 'x' 'c' 'v'
+'b' 'n' 'm' ',' '.' '/' $81 '*' $84 SPC $88 $90 $91 $92 $93 $94
+$95 $96 $97 $98 $99 $89 $8a '7' '8' '9' '-' '4' '5' '6' '+' '1'
+'2' '3' '0' '.' 0 0 0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+
+\ Same values, but shifted
+\ 0 1 2 3 4 5 6 7 8 9 a b c d e f
+create _mapupper $80 nc,
+0 0 '!' '@' '#' '$' '%' '^' '&' '*' '(' ')' '_' '+' BS $09
+'Q' 'W' 'E' 'R' 'T' 'Y' 'U' 'I' 'O' 'P' '{' '}' CR $82 'A' 'S'
+'D' 'F' 'G' 'H' 'J' 'K' 'L' ':' '"' '~' $80 '|' 'Z' 'X' 'C' 'V'
+'B' 'N' 'M' '<' '>' '?' $81 '*' $84 SPC $88 $90 $91 $92 $93 $94
+$95 $96 $97 $98 $99 $89 $8a '7' '8' '9' '-' '4' '5' '6' '+' '1'
+'2' '3' '0' '.' 0 0 0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+
+: _map _shift? if _mapupper else _maplower then ;
+
+\ Poll ps2@, apply shift logic, return the corresponding ASCII code.
+: ps2keyset1 ( -- c )
+ begin
+ begin ps2@? until ( kc )
+ dup $80 and swap $7f and _map + c@ ( released? c-or-0 )
+ dup $f0 and $80 = if ( released? c ) \ flags
+ $0f and swap if ( flg ) \ key released, unset flag
+ $fe swap lshift _flags and to _flags
+ else ( flg ) \ key pressed, set flag
+ 1 swap lshift _flags or to _flags
+ then ( ) 0
+ else ( released? c )
+ swap if \ released, do nothing
+ drop 0
+ else \ pressed
+ dup $80 and if drop '~' ( tmp placeholder for Fn keys ) then
+ then
+ then ( c-or-0 )
+ ?dup until ( c ) ;
diff --git a/fs/xcomp/bootlo.fs b/fs/xcomp/bootlo.fs
@@ -46,6 +46,7 @@
: Ac!+ Ac! A+ ;
: fill ( a u b -- ) A>r rot> >r >A begin dup Ac!+ next drop r>A ;
: allot0 ( n -- ) here over 0 fill allot ;
+: nc, ( n -- ) >r begin word runword c, next ;
\ Arithmetic
: > swap < ;
diff --git a/fs/xcomp/pc/init.fs b/fs/xcomp/pc/init.fs
@@ -7,6 +7,10 @@ f<< /drv/pc/acpi.fs
f<< /drv/pc/com.fs
f<< /drv/pc/vga.fs
f<< /sys/grid.fs
+f<< /drv/pc/pci.fs
+f<< /drv/pc/ps28042.fs
+f<< /sys/ps2.fs
' (emit) to emit
-' int16h to key
+' 8042ps2@? to ps2@?
+' ps2keyset1 to key
diff --git a/fs/xcomp/pc/kernel.fs b/fs/xcomp/pc/kernel.fs
@@ -26,27 +26,3 @@ xcode int13h ( drv head cyl sec dst -- ) pc lblcurrent pc>addr !
AX pspop, dh al mov, \ head
AX pspop, dl al mov, \ drive
$18 L1 jmpfar,
-
-\ TODO: move this out of BIOS. develop a proper driver for keyboard
-pc to L1 \ back to protected mode!
- ax $10 i) mov, ds ax mov, ss ax mov, es ax mov, gs ax mov, fs ax mov,
- ax ax xor, al bl mov, AX pspush,
- ret,
-
-pc to L2 1 to realmode \ we're in realmode
- ah ah xor, $16 int, bl al mov, \ result in BL
- \ we've done what we came for, let's go back to 32bit
- ax cr0 mov, ax 1 i) or, cr0 ax mov,
- $08 L1 jmpfar,
-
-pc to L1 \ segment with ffff limits
- ax $20 i) mov, ds ax mov, ss ax mov, es ax mov, gs ax mov, fs ax mov,
- ax cr0 mov, ax $fffffffe i) and, cr0 ax mov,
- 0 L2 jmpfar,
-0 to realmode
-
-xcode int16h ( -- c ) pc lblcurrent pc>addr !
- $18 L1 jmpfar,
-
-pc lblbootptr pc>addr !
-