LEDS work OK now! Feedback/code alts welcomed!<SOLVED>
Posted: Sun Jun 29, 2008 4:23 pm
See my last post
The Place to Start for Operating System Developers
https://f.osdev.org/
Code: Select all
do_LEDs:
call kbdp_wait
xor eax, eax
mov esi, kbd_ack
mov [esi], al ; clear the ack flag, so it can be tested
mov al, 0xed ; set LEDs command
out 0x60, al
; wait for an ACK on port 0x60 -- *kbd becomes unresponsive* until this ack is read from the port!
call do_block ; wait until kbd_ack != 0
; then send the status byte with proper bits set: Scroll = 1, Num = 2, Caps = 4
call kbdp_wait
mov al, [kb_sstat +1] ; grab the new LED status (shifted up 2)
mov [edx + led_old], al ; update "last known LED status"
shr eax, 2
out 0x60, al
ret
Code: Select all
void kblights()
{
int status = 1;
status = waiting(1);
if(status == 1)
{
outportb(0x60 , 0xED); //tried both 60 and 64 ports here, no kb lights come on
status = waiting(2);
}
}
int waiting(int checkno)
{
int notbusy = 1;
if(checkno == 1)
{
while(notbusy == 1)
{
if ((inportb(0x64) & 2) == 0) break; //is this 2 anded like binary port64 & 10
//which is the inputbufferfull flag thats getting checked here if 0? should this beif ((inportb(0x64) & 2) == 1) then? as 1 is input buffer full which means port 60h has data for reading? sorry if Im confused , finding info hard to comebuy for the kb Im afraid :(
}
return 1;
}
if(checkno == 2)
{
short mal;
while(notbusy == 1)
{
mal = inportb(0x64);
if((2 & mal) == 0) //trying to incorporate whats in brans guide and scatterings of keyboard info from many guides into a working solution in c
{
notbusy == 2;
//outputbuff empty so can write to port 60
outportb(0x60,0x7); //sending bits 0,1 and 2 set as 111 which means scroll lock,numlock,caps
puts("LED lights on !\n");
}
}
return 2;
}
puts("lights should be on now \n");
return notbusy;
}Port 0x64 and port 0x60 are both part of the keyboard controller. Sometimes you have to send or read bytes from one or the other. There is no rhyme, reason, or logic as to which or when. Some commands that you send (to port 0x60 sometimes, or port 0x64 sometimes) will generate an ACK that you must wait for.I'm really needing to now how and when 64h relates to 60h because from what Ive read I feel like Im trying to interpret half information at times
Code: Select all
void kblights()
{
short annoyed = 0;
while(annoyed == 0)
{
if ((inportb(0x64) & 2) == 0) break; //because input buff empty? i.e read buff, I dunno? very little guides with flag listings next to code
}
//set status indicators
outportb(0x60,0xED); //write to dataport
waitack(); //read from data port?
}
int waiting(int checkno)
{
unsigned char pa = 0;
unsigned char pb = 0;
//unused
}
void waitack()
{
unsigned char status = 0;
int outtahere = 1;
while(outtahere == 1)
{
status = (inportb(0x64));
if((status & 1) == 0) //hang on output buffer empty ready to write common !
{
puts("data ready! to be written but never gets in here \n");
outportb(0x60,0x7); //tried 0x7h and 0x07h here as well 7h = 111
outtahere = 0;
} else {
puts("NOT READY loops forever :( \n");
}
}
}Code: Select all
void kblights()
{
unsigned char al;
void kb_cmd_wait();
kb_cmd_wait();
outportb (0x60, 0xed);
// poll forever until the ACK byte from the kbd arrives
al= 0;
while (al != 0xfa){
// check if a kbd byte is available to read
if ( (inportb (0x64) & 1) !=0){
// if so, read it -- hopefully it's the ACK!
al= inportb (0x60);
}
}
kb_cmd_wait();
outportb (0x60, 0x7); // turn on all the lights
}
void kb_cmd_wait()
{
// poll forever waiting until bit 2 clears
while ( (inportb (0x64) &2) !=0) ;
}
Code: Select all
#include <system.h>
#define NUML 0x2
#define CAPSL 0x4
/* KBDUS means US Keyboard Layout. This is a scancode table
* used to layout a standard US keyboard. I have left some
* comments in to give you an idea of what key is what, even
* though I set it's array index to 0. You can change that to
* whatever you want using a macro, if you wish! */
unsigned char kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
CAPSL, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
NUML, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned char status;
volatile unsigned char flags[3] = {0,0,0,0};
volatile unsigned char tflags = 0;
volatile unsigned int mindex = 0;
/* Handles the keyboard interrupt */
void keyboard_handler(struct regs *r)
{
criticalregion(CSTOP);
unsigned char scancode,al,handled,cycle;
unsigned char numlock,capslock,scrolllock;
if(flags[0] == 0x0) {
puts("Back to zero");
}
/* Read from the keyboard's data buffer */
scancode = inportb(0x60);
/* If the top bit of the byte we read from the keyboard is
* set, that means that a key has just been released */
if (scancode & 0x80)
{
/* You can use this one to see if the user released the
* shift, alt, or control keys... */
}
else
{
/* Here, a key was just pressed. Please note that if you
* hold a key down, you will get repeated key press
* interrupts. */
/* Just to show you how this works, we simply translate
* the keyboard scancode into an ASCII value, and then
* display it to the screen. You can get creative and
* use some flags to see if a shift is pressed and use a
* different layout, or you can add another 128 entries
* to the above layout to correspond to 'shift' being
* held. If shift is held using the larger lookup table,
* you would add 128 to the scancode when you look for it */
if(kbdus[scancode] == '1')
{
testbwise();
}
if(kbdus[scancode] == 0x4) //capslock pressed
{
puts("in caps \n");
unsigned char caps = IsKeyOnOff(4);
ibfull(); //write
obfull(); //read
ibfull(); //write
tflags = flags[3];
outportb(0x60, tflags);
puts("CAPS SWITCH now! \n");
}
//*****************NUML SC
if(kbdus[scancode] == 0x2) //NUMlock pressed
{
puts("in num \n");
unsigned char caps = IsKeyOnOff(2);
criticalregion(CSTOP);
ibfull(); //write
obfull(); //read
ibfull(); //write
tflags = flags[3];
outportb(0x60, tflags);
puts("NUM SWITCHED now! \n");
}
if(flags[3] < 0)
{
puts("LESS THAN ZERO \n");
}
if(flags[3] > 0)
{
puts("GREATER THAN ZERO \n");
}
if(flags[3] == 0)
{
puts("TFLAGS is ZERO \n");
}
if(flags[3] == 1)
{
puts("TFLAGS is ONE \n");
}
if(flags[3] == 2)
{
puts("TFLAGS is TWO\n");
}
if(flags[3] == 3)
{
puts("TFLAGS is THREE \n");
}
if(flags[3] == 4)
{
puts("TFLAGS is FOUR\n");
}
if(flags[3] == 5)
{
puts("TFLAGS is FIVE\n");
}
if(flags[3] == 6)
{
puts("TFLAGS is SIX\n");
}
if(flags[3] == 7)
{
puts("TFLAGS is SEVEN\n");
}
if(flags[3] == 8)
{
puts("TFLAGS is EIGHT\n");
}
//*****************NUML END
}
putch(kbdus[scancode]);
puts("N \n"); //this is rotated just to make sure my make file is behaving
criticalregion(CSTART);
}
unsigned char ibfull()
{
while((inportb(0x64) & 2) != 0);
}
unsigned char obfull()
{
unsigned char al,info;
outportb (0x60, 0xED);
//rdy to write
al = 0;
while(al != 0xFA){
al= inportb (0x60);
info = al;
if ((inportb (0x64) & 1) != 0){
puts("READING DATA \n");
// if so, read it -- hopefully it's the ACK!
al= inportb (0x60);
if(al == 0xFF) {
puts("keyboard error \n");
}
if(al == 0x00) {
puts("Keyboard Error! To MANY KEYS PRESSED AT ONCE!\n");
}
if(al == 0xEE) {
puts("Result of an echo command \n");
}
if(al == 0xFC) {
puts("BAT Failed, go to petshop for new BAT N.B these errors are less meaningful than my comments! \n");
}
if(al == 0xFE){
puts("Resend data because Im a KB that cant handle data properly! \n");
}
}
}
return info;
}
unsigned char IsKeyOnOff(unsigned char keyTcheckONOFF) {
volatile unsigned char maskCL,maskNL,keep,cap,num;
int index;
//locflags = 0x00;
maskCL = 0x4;
maskNL = 0x2;
//locflags = flags[0];
keep = 0;
if(keyTcheckONOFF == 4)
{
puts("is capslock\n");
cap = flags[2];
if(cap == 0)
{
//caps is of turn on
puts("capslock is OFF turning on\n");
flags[2] = 4;
flags[3] = flags[3] + flags[2];
return 0;
}
if(cap == 4)
{
//caps is ON TURN OFF
puts("capslock is ON turning off\n");
flags[2] = 4;
flags[3] = flags[3] - flags[2];
flags[2] = 0;
return 0;
}
}
if(keyTcheckONOFF == 2)
{
puts("is numlock\n");
num = flags[1];
if(num == 0)
{
puts("numlock is OFF turning on \n");
flags[1] = 2;
flags[3] = flags[3] + flags[1];
return 0;
}
if(num == 2)
{
puts("numlock is ON turning off\n");
flags[1] = 2;
flags[3] = flags[3] - flags[1];
flags[1] = 0;
return 0;
}
}
}
/* Installs the keyboard handler into IRQ1 */
void keyboard_install()
{
irq_install_handler(1, keyboard_handler);
}
void criticalregion(unsigned char order)
{
if(order == CSTART)
{
__asm__ __volatile__ ("sti");
}else if(order == CSTOP)
{
__asm__ __volatile__ ("cli");
}else if(order != CSTART | CSTOP)
{
puts("Incorrect Parameter! to critical region function \n");
}
}
void testbwise()
{
unsigned char ctest = 0;
puts("lets try BW test \n");
ctest = 0x04;
if((ctest>> 2) == 1)
{
puts("ctest value initially was 4 but move right 2 equ 1\n");
ctest++;
if(ctest == 5) {
puts("shifting in if doesnt affect value permenant \n");
}
ctest = ctest >> 1;
if(ctest == 2)
{
puts("value five shifted 1 is two WAYA\n");
}
}
}Code: Select all
Cps Num Scrll
4 2 1
1 0 0
So the above situation I thought was dealt with but it wasn't I always checked for numlock if(scancode == NUML && flags[0] >> 1 == 0)
THEN
numlock is off.... heres the thing though by looking at what I did in binary above with this specific situation even though numlock is 0 it still registers the value 2 because
flags[0] >> 1 =
Cps Num Scrll
4 2 1
0 1 0<------this was the numlock value I moved along but I wasnt accounting that the actuial value to compare irrespective of numlock bit 1 being 0 was 0x2
so that needed to be added as one of the extra conditions, it seems VERY un-elegant but heck it helped me to the extent I will be using bitwise operators alot in my code now so its good!
//code START
//now for a very unreadable code way to do it :)
#include <system.h>
#define NUML 0x2
#define CAPSL 0x4
/* KBDUS means US Keyboard Layout. This is a scancode table
* used to layout a standard US keyboard. I have left some
* comments in to give you an idea of what key is what, even
* though I set it's array index to 0. You can change that to
* whatever you want using a macro, if you wish! */
unsigned char kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
CAPSL, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
NUML, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
volatile unsigned short flags[0]={0};
/* Handles the keyboard interrupt */
void keyboard_handler(struct regs *r)
{
criticalregion(CSTOP);
volatile unsigned char scancode,al,handled,cycle;
volatile unsigned char numlock,capslock,scrolllock;
if(flags[0] == 0x0) {
puts("Back to zero");
}
/* Read from the keyboard's data buffer */
scancode = inportb(0x60);
/* If the top bit of the byte we read from the keyboard is
* set, that means that a key has just been released */
if (scancode & 0x80)
{
/* You can use this one to see if the user released the
* shift, alt, or control keys... */
}
else
{
/* Here, a key was just pressed. Please note that if you
* hold a key down, you will get repeated key press
* interrupts. */
/* Just to show you how this works, we simply translate
* the keyboard scancode into an ASCII value, and then
* display it to the screen. You can get creative and
* use some flags to see if a shift is pressed and use a
* different layout, or you can add another 128 entries
* to the above layout to correspond to 'shift' being
* held. If shift is held using the larger lookup table,
* you would add 128 to the scancode when you look for it */
if(kbdus[scancode] == '1')
{
testbwise();
}
if(kbdus[scancode] == 0x4) //capslock pressed
{
puts("in caps \n");
unsigned char caps = IsKeyOnOff(CAPSL);
ibfull(); //write
obfull(); //read
ibfull(); //write
outportb(0x60, flags[0]);
puts("CAPS SWITCH now! \n");
}
//*****************NUML SC
if(kbdus[scancode] == 0x2) //NUMlock pressed
{
puts("in num \n");
unsigned char caps = IsKeyOnOff(NUML);
ibfull(); //write
obfull(); //read
ibfull(); //write
outportb(0x60, flags[0]);
puts("NUM SWITCHED now! \n");
}
//*****************NUML END
}
putch(kbdus[scancode]);
puts("N \n"); //this is rotated just to make sure my make file is behaving
}
unsigned char ibfull()
{
while((inportb(0x64) & 2) != 0);
}
unsigned char obfull()
{
unsigned char al,info;
outportb (0x60, 0xED);
//rdy to write
al = 0;
while(al != 0xFA){
al= inportb (0x60);
info = al;
if ((inportb (0x64) & 1) != 0){
puts("READING DATA \n");
// if so, read it -- hopefully it's the ACK!
al= inportb (0x60);
if(al == 0xFF) {
puts("keyboard error \n");
}
if(al == 0x00) {
puts("Keyboard Error! To MANY KEYS PRESSED AT ONCE!\n");
}
if(al == 0xEE) {
puts("Result of an echo command \n");
}
if(al == 0xFC) {
puts("BAT Failed, go to petshop for new BAT N.B these errors are less meaningful than my comments! \n");
}
if(al == 0xFE){
puts("Resend data because Im a KB that cant handle data properly! \n");
}
}
}
return info;
}
unsigned char IsKeyOnOff(unsigned char keyTcheckONOFF) {
unsigned char maskCL,maskNL,keep;
int index;
//locflags = 0x00;
maskCL = 0x04;
maskNL = 0x02;
//locflags = flags[0];
keep = 0;
if(keyTcheckONOFF == CAPSL)
{
//is capslock so check if on - if so turn off
if((flags[0] >> 2) == 0x1)
{
//comes in on
//turn off
flags[0] = flags[0] ^ 0x4;
if((flags[0] >> 1) == 2)
{
puts("flags IS TWOafter caps turned ON\n");
}
if(flags[0] == 0x1)
{
puts("flags is 1\n");
}
if(flags[0] == 0x2)
{
puts("flags is 2\n");
}
if(flags[0] == 0x4)
{
puts("flags is 4\n");
}
if(flags[0] == 0x6)
{
puts("flags is 6\n");
}
if(flags[0] > 0x6)
{
puts("Greater than SIX \n");
}
if(flags[0] < 0x0)
{
puts("LESS than ZERO \n");
}
if(flags[0] > 0x0)
{
puts("GREAT than ZERO \n");
}
if(flags[0] == 0x0)
{
puts("EQUAL ZERO \n");
}
puts("CAPSOFFFFF \n");
return 0;
}
//is capslock so check if off - if so turn on
if((flags[0] >> 2) == 0x0 )
{
//comes in off
flags[0] = (flags[0] | 0x4);
//flags[0] = locflags;
if(flags[0] == 0x1)
{
puts("flags is 1\n");
}
if(flags[0] == 0x2)
{
puts("flags is 2\n");
}
if(flags[0] == 0x4)
{
puts("flags is 4\n");
}
if(flags[0] == 0x6)
{
puts("flags is 6\n");
}
puts("Caps gets turned ON flags set to XARGHH\n");
return 0;
}
}
//C N S
//4 2 1
//1 0 0 CON
//shr 1
//1 1 0
//1 1 0 CON,NON
//***************NUML
if(keyTcheckONOFF == NUML)
{
puts("YesNUM but need more \n");
unsigned char tempnl = flags[0];
//is NUMlock so check if on - if so turn off
if((flags[0] >> 1) == 0x1)
{
flags[0] = (flags[0] ^ 0x2);
puts("NUML Hopefully ObFFFffffb \n");
//flags[0] = locflags;
if(flags[0] == 0x1)
{
puts("flags is 1\n");
}
if(flags[0] == 0x2)
{
puts("flags is 2\n");
}
if(flags[0] == 0x4)
{
puts("flags is 4\n");
}
if(flags[0] == 0x6)
{
puts("flags is 6\n");
}
if(flags[0] < 0x0)
{
puts("LESS than ZERONMAO \n");
}
if(flags[0] > 0x0)
{
puts("GREAT than ZEROAO \n");
}
if(flags[0] == 0x0)
{
puts("EQUAL ZEROAO \n");
}
return 0;
}
if((flags[0] >> 1) == 0x0)
{
flags[0] = (flags[0] | 0x2);
puts("NUML gets turned ONNNNN\n");
//flags[0] = locflags;
if(flags[0] == 0x1)
{
puts("flags is 1\n");
}
if(flags[0] == 0x2)
{
puts("flags is 2\n");
}
if(flags[0] == 0x4)
{
puts("flags is 4\n");
}
if(flags[0] == 0x6)
{
puts("flags is 6\n");
}
return 0;
}
//100
//010 = 2
if((flags[0] >> 1) == 0x2)
{ //numlock is off to come on when caps is on
//4 or 2
//110
//010
//110
flags[0] = (flags[0] | 0x2);
puts("NUM turned ON \n");
return 0;
}
if(flags[0] >> 1 == 0x3)
{
flags[0] = (flags[0] ^ 0x2);
puts("Num turned OFF \n");
return 0;
}
//
if((flags[0] >> 1) < 0)
{
puts("flags less than zero \n");
}
if((flags[0] >> 1) > 0)
{
puts("flags greater than zero \n");
}
if((flags[0] >> 1) == 2)
{
puts("flags IS TWOaa\n");
}
}
//**************NUML END
if(keyTcheckONOFF != NUML){
puts("NotEQUAL NUML \n");
}
if(keyTcheckONOFF != CAPSL){
puts("NotEQUAL CAPSL \n");
}
if(keyTcheckONOFF == CAPSL){
puts("EQUAL CAPSL but problem \n");
}
if(keyTcheckONOFF == NUML){
puts("EQUAL NUML but problem \n");
}
puts("problem if gets here \n");
}
/* Installs the keyboard handler into IRQ1 */
void keyboard_install()
{
irq_install_handler(1, keyboard_handler);
}
void criticalregion(unsigned char order)
{
if(order == CSTART)
{
__asm__ __volatile__ ("sti");
}else if(order == CSTOP)
{
__asm__ __volatile__ ("cli");
}else if(order != CSTART | CSTOP)
{
puts("Incorrect Parameter! to critical region function \n");
}
}
void testbwise()
{
unsigned char ctest = 0;
puts("lets try BW test \n");
ctest = 0x04;
if((ctest>> 2) == 1)
{
puts("ctest value initially was 4 but move right 2 equ 1\n");
ctest++;
if(ctest == 5) {
puts("shifting in if doesnt affect value permenant \n");
}
ctest = ctest >> 1;
if(ctest == 2)
{
puts("value five shifted 1 is two WAYA\n");
}
}
}
//code END