LEDS work OK now! Feedback/code alts welcomed!<SOLVED>

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

LEDS work OK now! Feedback/code alts welcomed!<SOLVED>

Post by eax »

See my last post
Last edited by eax on Sun Jul 06, 2008 7:24 pm, edited 7 times in total.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Looking for comprehensive KB guide

Post by bewing »

You have to send another byte after the 0xED, to tell the kbd which LEDs to turn on/off.

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
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Looking for KB test help(struggling in C)

Post by eax »

I'm actually wanting to do it in C and Im not sure what you mean by the acknowledge flag, I thought I would be better of when I read the keyboard guides at OS dev but I have managed to find them after all and at least to my mind they don't explain the exact process it goes through giving lots of commands and examples of how their flags would be set per step. The only other remotely decent piece of information I could find on the keyboard was art of assembly chapter 20 approx and it didn't align the bits properly to the diagram, at least I think its a mistake? because it should bits 0 to 7 after a blank square? so 9 bits?

Anyway heres the code I came up with in C trying to take into consideration of hat Ive been told plus the limited amount of well worded docs on the keyboard out there.

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;
}
Yes the code is bad in the sense of structure but once I manage to actually understand whats happening, i.e which flags are set, which registers are getting read and written from at which time as well as which ports these relate to I will try and tidy it up:) BTW just to repeat I did find keyboard docs on OSDev , I imagine they will be really quick for the experianced but not near as much depth as the other guides Ive read here. (Just in case people think Im expecting being spoon fed)

Im 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:)

Initially I just want to poll the keyboard to turn lights on then after Im happy with this I will turn them on when irq 1 is called the first time or something like that
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Looking for comprehensive KB guide

Post by bewing »

I will probably rewrite the keyboard entry in the wiki at some point to make it all clearer. But the point with the LEDs is that it is a two stage process. Here it is in english:

1) Wait on port 0x64 until it says that the keyboard controller is ready to accept a command. This may require reading bytes from port 0x60 until the keyboard buffer is flushed. It will involve polling in your original version. It gets much more complicated if you have to do multitasking blocking instead, for many reasons.
2) Write the 0xED (Set LEDs) command byte to port 0x60.
3) Wait for the ACK byte (0xFA) to come back from the keyboard on port 0x60. -- This is quite important. It will involve polling in your original version. It gets much more complicated if you have to do multitasking blocking instead.

Please note that this byte will cause an IRQ1, and if your keyboard driver is turned on, it will race you to get the byte from port 0x60, if you are also polling port 0x60. It will win most of the time. You may need to rewrite your keyboard driver to set a flag every time it sees a 0xFA ACK byte come in on port 0x60. Then your routine would poll the flag instead.

4) Write one more byte to port 0x60. The low three bits set the LEDs on or off. All the other bits should be 0. If the bits are set, the LEDs turn on. Bit values: Scroll = 1, Num = 2, Caps = 4.
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
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.

As far as port 0x64 goes -- when you read it, you get what I call a "keyboard status byte":
bit 0, value = 1: set if there is a byte waiting to be read from port 0x60
bit 1, value = 2: clear if controller is ready to accept a command
bit 5, value = 0x20: the byte available on port 0x60 came from the secondary PS2 port (mouse)
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Re: Looking for comprehensive KB guide

Post by eax »

Hiya thanks for the reply and the recent addition about the bits :), thats fair comment about the logic of it which then means its a matter of just battering the lots of different rules per situation into my head lol

Anyway Ive been trying my best with what you said , Im not sure if the way Ive handled the ack command is correct but heres a little tidyer code but what happens is it loops and says not aknowledged so my variable is never equal to ack, I tried my var as an unsigned char and short and got same result, heres the code

EDIT:Updated code based on info from a good lot of places but still no joy :( , it never acknowledges, what is that in practice? an OBF full or empty at command port 64h?

Also not that I can afford it just now but where can I get a really good book that covers this stuff?

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");


	}
}
}
excuse my badly named variable in the while loop , have to keep myself sane somehow :) since I cant find C code out there,

Just one final thing before I give it a rest for tonight :) and just to say I do really appreciate the theoretical steps given but what does

"Polling in your original version mean" original version of what? If were talking a line of c code would it be possible you could say what line that is or if its ports could you list the hexport in question? btw I know what polling is thats not what I have trouble with its just at that point in the steps exactly what the reference is to.

some actual code that shows the ack block in practice would be nice as well as I havent seen this anywhere. At this point even getting it in pure asm would be great, until then Im stuck on guesswork. BTW the reason for all these additions is because Im making sure Im doing my part and reading everything over and over and now Im getting an idea of the grey areas :)


Thanks for reading :)
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: still cant get LEDS but think Im closer but need help in C

Post by bewing »

OK, one last time. :wink:
To make this work in this way, you MUST make sure that the IRQ1 handler is DISABLED -- most likely by shutting off IRQ1 at the PIC.

This code needs timeouts, and kb_cmd_wait could be a little smarter.

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) ;
}
The whole point about polling is that you cannot use it in multitasking mode. It will eat your cpu. So you will have to completely rewrite all of this later, not to use polling at all.
eax
Member
Member
Posts: 57
Joined: Mon Jun 23, 2008 6:45 am

Leds work via interrupts now!

Post by eax »

Ok :) I have both numlock and capslock lights turning off now as they should turn on and off! What I don't like is I had to do it a very very basic way with arrays but hey it works!

If anyone in future can improve this to incorporate bitwise operators instead of using lots of array slots please feel free to add, also all this code was to do was for me to see the lights going on and off and that I could at least code that, so mission accomplished and its kinda helped me understand how this way of doing it is flawed in many ways i.e disabling ints for the whole of the KB handler , also as mentioned I probably need timeouts but Im not to sure how I would implement that + my code assumes all lights start as OFF(Well num and caps) and this is bad, if I knew a way to get the current lights status from the KB ports it would be better. Anyway heres the code but I really really wanted my way using bitwise operators to work

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");
}
}
 


}
Im pretty certain tomorrow Im not going to get bogged down with this now that Ive achieved what I intended but please if anyone knows a better way and can provide easy to read code please add, actually it doesn't even need to be easy to read :) so feel free! Also constructive criticism with references that will show me how to go about things differently by providing code examples are welcome! or just general advice. Its just this thread has had a monster amount of views so Im sure some decent code submitted here would benefit the scores of people wanting to know about this. (Have even contacted an author from one of the many books I have for advice on this :)) BTW If I google for C LED code , most search variations on Google come up with this post high up or at the toplol

--------------------------AN alternate Messy as Hell WAY further below

firstly this was the initial way I tried it because doing brans guide made me realise bitwise operations should be second nature to me. Dont get me wrong on paper I can do them its just up until brans guide sometimes I could mess them up in code. This piece of code was done when I had some assumptions about bitwise operations in my code that were wrong , so suffice to say It could be done much much better and in a more elegant way(in hindsight). Still I got this code working now and Im 100 times happier about bitwise operations now! If anything I reckon all this code is good for is to make sure you understand what effects ors and xors coupled with binary shifts are having :) The reason it didnt work initially was because when numlock scancode........ capslock was on and numlock was off we had the values

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



Anyway this is definatly my last post on this! and Im aware scroll lock isnt implemented. I will be doing a complete re-write when I get to the stage all the main things are done in my OS and don't see the point in doing the KB when theres alot more important things like memory management which I am finally going to look into! When I looked at KB code I was grade A clueless lol and thats why I wanted to set myself to do what some may see as a pointless exercise :) ^^ Theres how not to do it at least hehe
Post Reply