Skip to content

Commit

Permalink
Merge pull request #1133 from ajcasado/master
Browse files Browse the repository at this point in the history
Floppy driver fix, video optimization, keyboard scan optimization and other minor fixes.
  • Loading branch information
EtchedPixels authored Jan 8, 2025
2 parents f689dff + f8404be commit ebe2f95
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 458 deletions.
137 changes: 3 additions & 134 deletions Kernel/dev/cpc/cpckeyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,139 +159,8 @@ void tty_pollirq(void)

static uint8_t update_keyboard(void) __naked
{
__asm

ld bc, #0xF782 ;; [3] Configure PPI 8255: Set Both Port A and Port C as Output.
out (c), c ;; [4] 82 = 1000 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=0)=> Port A=Output, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0,(B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
ld bc, #0xF40E ;; [3] Write (0Eh = 14) on PPI 8255 Port A (F4h): the register we want to select on AY-3-8912
ld e, b ;; [1] Save F4h into E to use it later in the loop
out (c), c ;; [4]

ld bc, #0xF6C0 ;; [3] Write (C0h = 11 000000b) on PPI Port C (F6h): operation > select register
ld d, b ;; [1] Save F6h into D to use it later in the loop
out (c), c ;; [4]
.dw #0x71ED ; out (c), 0 ;; [4] out (C), 0 => Write 0 on PPI's Port C to put PSG's in inactive mode
;; .... (required in between different operations)
ld bc, #0xF792 ;; [3] Configure PPI 8255: Set Port A = Input, Port C = Output.
out (c), c ;; [4] 92h= 1001 0010 : (B7=1)=> I/O Mode, (B6-5=00)=> Mode 1,
;; (B4=1)=> Port A=Input, (B3=0)=> Port Cu=Output,
;; (B2=0)=> Group B, Mode 0, (B1=1)=> Port B=Input, (B0=0)=> Port Cl=Output
;; Read Loop (Unrolled version): We read the 10-bytes that define the pressed/not pressed status
;;
ld a, #0x40 ;; [2] A refers to the next keyboard line to be read (40h to 49h)
ld hl, #_keybuf ;; [3] HL Points to the start of the keyboardBuffer,
;; ... where scanned data will be stored

;; Read line 40h
ld b, d ;; [1] B = F6h => Write the value of A to PPI's Port C to select next Matrix Line
out (c), a ;; [4]
ld b, e ;; [1] B = F4h => Read from PPI's Port A: Pressed/Not Pressed Values from PSG
ini ;; [5] The read value is written to (HL), then HL<-HL+1 and B<-B-1
inc a ;; [1] Loop: Increment A => Next Matrix Line.

;; Read line 41h
ld b, d ;; [1] Same for line 41h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 42h
ld b, d ;; [1] Same for line 42h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 43h
ld b, d ;; [1] Same for line 43h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 44h
ld b, d ;; [1] Same for line 44h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 45h
ld b, d ;; [1] Same for line 45h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 46h
ld b, d ;; [1] Same for line 46h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 47h
ld b, d ;; [1] Same for line 47h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 48h
ld b, d ;; [1] Same for line 48h
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]
inc a ;; [1]

;; Read line 49h
ld b, d ;; [1] Same for line 49h (Except we don't have to increase a anymore)
out (c), a ;; [4]
ld b, e ;; [1]
ini ;; [5]

;; Restore PPI status to Port A=Output, Port C=Output
;;
ld bc, #0xF782 ;; [3] Put again PPI in Output/Output mode for Ports A/C.
out (c), c ;; [4]

ld hl, #_keybuf ;; [3] HL Points to the start of the keyboard status buffer
ld b, #9 ;; [2] We are going to do 9 AND operations against the first byte of the buffer
ld a, (hl) ;; [2] A = First byte from keyboardStatusBuffer

inc hl ;; [2] HL points to the next byte from the KeyboardStatusBuffer
and (hl) ;; [2] A = A & NextByte (The byte pointed by HL)
inc hl ;; [2] Repeat for byte Buffer+2
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+3
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+4
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+5
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+6
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+7
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+8
and (hl) ;; [2]
inc hl ;; [2] Repeat for byte Buffer+9
and (hl) ;; [2]

inc a ;; [1] A holds the result of ANDing the 10 bytes. If no key is pressed, all bits should
;; ... be 1, so A=0xFF. If we add 1, A=0, we return FALSE (no key is pressed).
;; ... If any key is pressed, some bit will be 0, so A != 0xFF, which means A+1 != 0, and
;; ... we will be returning TRUE (A > 0)
ld l, a ;; [1] L = A (Set return value for C calls in L)
ret
__endasm;
}
/*{
//from https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_scanKeyboard_if.s
//and https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_isAnyKeyPressed_f.s
/*from https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_scanKeyboard_if.s
and https://github.com/lronaldo/cpctelera/blob/master/cpctelera/src/keyboard/cpct_isAnyKeyPressed_f.s*/
__asm

;; Configure PPI: Select Register 14 (the one connected with keyboard status) and set it for reading
Expand Down Expand Up @@ -436,4 +305,4 @@ __endasm;
ret ;; [3] Return

__endasm;
}*/
}
Loading

0 comments on commit ebe2f95

Please sign in to comment.