OS Dev PIC and IRQ's

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.
Cjmovie

OS Dev PIC and IRQ's

Post by Cjmovie »

It seems that I cant find a simple enough tutorial on the PIC or IRQ's. Really, I cant get very far like this ( >:(). I want to develope a very simple GUI based OS, but I cant seem to get this crucial info.

*not about PIC/IRQ*
What do you think of this very simple malloc idea?
Asked for Memory: Get next ram position
Check to see if its used, if so search again
Write to Ram: (word)size, (byte)0x00
Give Address
Add # used memory to next ram variable.

Simple free idea:
Goto free address
Subtract 3 bytes (get info area)
get size
clear out mem to 0
clear size to 00, byte to 0x01
change next pos to old pos
Therx

Re:OS Dev PIC and IRQ's

Post by Therx »

clear out mem to 0
that'll be very slow

Pete
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

Pete wrote:
clear out mem to 0
that'll be very slow
Actually, the whole thing would be very slow. But it would be quite space efficient, which is what I assume the author is striving for.

As far as the clearing out freed memory to 0, there's really no reason to do this inside a malloc implementation. Now, it may be necessary in your kernel's page allocator, if you're concerned about security (e.g. process E requests some core, then examines it's contents to see what process A or B was doing). I do something like this myself, except instead of clearing a page when it's freed, I clear it when it's allocated if it's being allocated to a user process rather than a system process. Thus, I avoid clearing the memory except when it really is being handed off to an untrusted app.

But again, notice I'm talking about the page allocator, that gives pages of memory to apps. A malloc routine is a routine that manages memory within a single process' address space, there's really no reason a malloc routine would to this, except for the convenience of a program that indicates it wants the memory cleared (which it does by calling calloc).
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

Note: If you really are striving for space efficiency, and you can guarentee that you never malloc structures larger than 32K, you don't need to follow the size word with a status byte for used/free flagging. Instead, you can flag this using a bit in the size word itself. I've considered this before, as my kernel doesn't malloc anything much larger than 1514 bytes (for really large structures, I just go straight to the page allocator). Alternately, you can go with structures as large at 64K and use the low bit to indicate used or free, if you keep all your blocks word aligned.
Cjmovie

Re:OS Dev PIC and IRQ's

Post by Cjmovie »

OK, thanks for the Re-Plys.
1. Im settin everything to 0 so that if an app forgets to set an end, for, say, some string etc., it wont hang the system for a minute and then crash trying to do some routine on it. Mainly, because I do this a lot (:-P).

2. I set that to 0 (the byte) so the malloc can make sure another program didnt some-how goof with the memory (for now Im ignorig a lot of features of Pmode, setting it for use by anything(the memory)).
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

Cjmovie wrote:1. Im settin everything to 0 so that if an app forgets to set an end, for, say, some string etc., it wont hang the system for a minute and then crash trying to do some routine on it. Mainly, because I do this a lot (:-P).
This will make your system less reliable, since it will cause otherwise obvious bugs to (sometimes) pass unnoticed. If you make this kind of error often, you should be coming up with strategies to make catching the errors easier, not more difficult.
Cjmovie wrote:2. I set that to 0 (the byte) so the malloc can make sure another program didnt some-how goof with the memory (for now Im ignorig a lot of features of Pmode, setting it for use by anything(the memory)).
Um, I can see the problem, I'm just baffled as to why you think this would help solve it? Even if a program did start messing with memory it shouldn't, it'd be a billion to one shot against it actually hitting that particular byte.
Cjmovie

Re:OS Dev PIC and IRQ's

Post by Cjmovie »

I see where your going.....Hmm, I could make the size area resizable and it ends always with a byte of 0x00....Leaving the ability still to have even more space. To solve the problem of having, say 0x55 and 0x00, I might increase it just a little to 0x55 and 0x01.
Anyway, We've gotten way of the major topic - I still need the base info, as I have a really nice GUI, but no way to interact with it :-P. Maybe also some simple tutorials, then, on the ports for mice and keyboard? Does Int 0x33 work without MS Win? Does int 0x23 (I think thats it....) work without Windows? I really, though, just am in need of inputs. Graphics, that I can do.
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

There are BIOS functions for reading the keyboard IIRC, although you can't use them from protected mode, and bouncing into/out of Virtual-8086 mode for every darn keypress would be ill advised. And there's no BIOS functions for the mouse, that's why DOS always needed mouse drivers in your CONFIG.SYS. You'll have to implement that yourself.

I think we got off-topic here because you failed to actually ask any on-topic questions in your first post. The ONLY question you asked was, "What do you think of this very simple malloc idea?"

You've stated you need to know something about the PIC and IRQ's, but that's not only not a question, it's a pretty darn vague statement. What do you need to know? Knowing no more than what you're said so far, all I can recommend is, go read the FAQ. If there's some question that isn't answered there, you're going to have to tell us what the question is before we'll have a shot at answering it...
Cjmovie

Re:OS Dev PIC and IRQ's

Post by Cjmovie »

Maybe Im going all out of order here, but I'll try to get a straightfoward question out:

Can somebody give me an example in ASM(or C) of how to enable the mouse to generate an Interrupt and then get the button and movement data from it?

I guess its just so, since Im only 13, that I have trouble in these things..... ;).
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

You're going to need some OS infrastructure already in place to do that (a properly setup IDT table, for example), but as far as setting up the mouse to generate interrupts, and then handling the interrupts:

Code: Select all

#define INPUT_REG   0x60  // read only
#define OUTPUT_REG  0x60  // write only
#define CONTROL_REG 0x64  // write only
#define STATUS_REG  0x64  // read only

// Commands for the control register
#define WRITE_CONTROLLER         0x60
#define DISABLE_AUXILIARY_DEVICE 0xA7
#define ENABLE_AUXILIARY_DEVICE  0xA8
#define WRITE_AUXILIARY_DEVICE   0xD4

// Values for the controller
#define ENABLE_INTERRUPTS  0x47
#define DISABLE_INTERRUPTS 0x45

// Commands for the mouse
#define RESET_SCALING         0xE6
#define SET_SCALING           0xE7
#define SET_RESOLUTION        0xE8 // followed by resolution (1 byte)
#define SET_SAMPLE_RATE       0xF3 // followed by sample rate (1 byte)
#define ENABLE_DATA_TRANSFER  0xF4
#define DISABLE_DATA_TRANSFER 0xF5
#define SET_STANDARD          0xF6
#define RESET_MOUSE           0xFF

INLINE int psAuxWait(int mask, int value)
{
   int i = 100000;
   while ( i-- ) if ( (inportb(STATUS_REG) & mask) == value ) return 1;
   return 0;
}

INLINE void psAuxCommand1(int c1)
{
   psAuxWait(2, 0);
   outportb(CONTROL_REG, c1);
}

INLINE void psAuxCommand2(int c1, int c2)
{
   psAuxWait(2, 0);
   outportb(CONTROL_REG, c1);
   psAuxWait(2, 0);
   outportb(OUTPUT_REG, c2);
}

static int state;
static UInt8 buttons;
static SInt8 delta_x, delta_y;

void handlePSAuxInt(void)
{
   switch ( state )
   {
    case 0:
      buttons = inportb(INPUT_REG);
      if ( !(buttons & 0xC0) ) ++state;
      break;
    case 1:
      delta_x = inportb(INPUT_REG);
      ++state;
      break;
    case 2:
      delta_y = inportb(INPUT_REG);
      state = 0;
      
      if ( delta_x || delta_y || (buttons & 7) != pointer.buttons )
      {
         pointer.x += delta_x;
         if ( pointer.x < bounds.left )
            pointer.x = bounds.left;
         else if ( pointer.x > bounds.right )
            pointer.x = bounds.right;
         
         pointer.y -= delta_y;
         if ( pointer.y < bounds.top )
            pointer.y = bounds.top;
         else if ( pointer.y > bounds.bottom )
            pointer.y = bounds.bottom;

         pointer.buttons = buttons & 7;
         
         ++pointer.movement;
      }
   }
   outportb(0xA0, 0x20);
   outportb(0x20, 0x20);
}

extern void catchPSAuxInt(void); // asm stub that calls handlePSAuxInt

void setupPSAux(void)
{
   state = pointer.x = pointer.y = pointer.buttons = 0;
   bounds.left = bounds.top = 0;
   bounds.right = 639;
   bounds.bottom = 479;
   
   kprintf("PS/2 Auxiliary Port driver\n");
   
   psAuxCommand1(ENABLE_AUXILIARY_DEVICE);
   psAuxCommand2(WRITE_CONTROLLER, ENABLE_INTERRUPTS);
   psAuxCommand2(WRITE_AUXILIARY_DEVICE, SET_STANDARD);
   psAuxCommand2(WRITE_AUXILIARY_DEVICE, ENABLE_DATA_TRANSFER);
   
   setIRQHandler(12, catchPSAuxInt);
   enableIRQ(12);
}
That's all the hardware specific code you need to handle a PS/2 mouse -- the rest (setting up the IDT, enabling IRQ's, etc.) you should be able to do with the information in the FAQ wiki...
Cjmovie

Re:OS Dev PIC and IRQ's

Post by Cjmovie »

Well.....maybe I could get a fully C compatable ASM function? That asks the mouse for the stats this time, if thats simpler (I want it to work, then work good and fast....).

Also, I really didnt find that much in the OS Dev wikki...

Right now my source code is basically the graphics, a VERY VERY simple malloc/free (which for some reason crashes the system.....Any Ideas? (malloc and free both crash)). Also is a very simple Print(char* text, char** fontPTR, int x, int y); with a font to go along with it, and the code to handle the mouse once it works.........

Maybe Im going about this in the wrong way? IDK, I mean, I did make a simple command based OS before this, but the keyboard is so much easier than the mouse ( :o).
Dreamsmith

Re:OS Dev PIC and IRQ's

Post by Dreamsmith »

Keep in mind, the mouse doesn't know its position. It doesn't have an X and Y in its little head, which it updates on its own and has ready to give you when you ask for it. All it knows is that it has moved so far over some time interval. I'm not sure if it's even possible to handle a mouse using polling rather than interrupts. If it is possible, I haven't a clue how to go about it...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:OS Dev PIC and IRQ's

Post by Brendan »

Hi,
Dreamsmith wrote: I'm not sure if it's even possible to handle a mouse using polling rather than interrupts. If it is possible, I haven't a clue how to go about it...
Sure it is :) - the PS/2 mouse is almost exactly the same as the PS/2 keyboard (continuously sample the "output buffer full" flag for port B).

A serial mouse uses the serial port (!) which can also be polled by continuously sampling the "data ready" flag in the line status register (although don't expect it to work with higher baud rates - mouse is only 9600 bps).

Why you'd want polling to start with is left for the reader to figure out ::).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Cjmovie

Re:OS Dev PIC and IRQ's

Post by Cjmovie »

OK, I figured that, but does anyone have some example code to show how to do that with the 'in' command?

*Intel Syntax* --Whisper.......

With all this trouble, It seems odd they would make 100's of tutorials for getting it to boot, then have few, comparatively, that go past that.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:OS Dev PIC and IRQ's

Post by bubach »

Hello, i am also at the point when i am going to implement INT?s, and i used:

http://www.osdever.net/tutorials/interr ... ?the_id=39
and
http://www.osdever.net/tutorials/interr ... ?the_id=40
and
http://www.osdever.net/tutorials/interr ... ?the_id=41

not that it works for me, but u problably write your os in c and would have much more help of the example kernel here:

http://osdev.neopages.net/downloads/tuts/IDTkernel.zip

hope it can help you.
/ Christoffer
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply