The code I tried to adapt is below , basically when I press the number one on the KB it reads a sector and when I press 2 its supposed to write a sector, however it freezes, also should the drive variable contain a 0 for a floppy? and what should the temp var contain? (How do I get it)
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 vardbus[0] = {'*'};
unsigned char buffer[];
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')
{
unsigned char addr = 0x0;
unsigned char drive = 0;
unsigned char idx = 0;
unsigned char tmpword = 0;
//read sector LBA28
outportb(0x1F1, 0x00);
outportb(0x1F2, 0x01);
outportb(0x1F3, (unsigned char)addr);
outportb(0x1F4, (unsigned char)addr >> 8);
outportb(0x1F5, (unsigned char)addr >> 16);
outportb(0x1F6, 0xE0 | (drive << 4) | ((addr >> 24) & 0x0F));
outportb(0x1F7, 0x20);
while (!(inportb(0x1F7) & 0x08)) {}
//And then read/write your data from/to port 0x1F0:
// for read:
for (idx = 0; idx < 256; idx++)
{
tmpword = inw(0x1F0);
buffer[idx * 2] = (unsigned char)tmpword;
buffer[idx * 2 + 1] = (unsigned char)(tmpword >> 8);
}
}
if(kbdus[scancode] == '2')
{
//check drive
outportb(0x1F6, 0xA0); // use 0xB0 instead of 0xA0 to test the second drive on the controller
timer_wait(1);
unsigned short tmpword = inportb(0x1F7); // read the status port
if (tmpword & 0x40) // see if the busy bit is set
{
puts("Primary master exists\n");
}
}
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");
}
}
//now for timer.c
void timer_wait(int ticks)
{
//is called for 1 tick to allow for timeout
unsigned long eticks;
eticks = timer_ticks + ticks;
while(timer_ticks < eticks);
}
}