PIC Problem [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
newwen
Posts: 16
Joined: Sun Feb 03, 2008 3:04 am

PIC Problem [SOLVED]

Post by newwen »

Hello, I am working on remapping pic and I am having some unknown problems with my C code. Right now the program displays a message once main has been given control.

Code: Select all

void main()
{
cls(0x07);
printf(0x07, "Hello",1,1);
for(;;);
}
Whenever I add this code:

Code: Select all

void remap_pics()
{
	/* send ICW1 */
	out(0x20,  0x11);
	out(0xA0,  0x11);

	/* send ICW2 */
	out(0x20 + 1, 0x20);	//master
	out(0xA0 + 1, 0x28);	//slave

	/* send ICW3 */
	out(0x20 + 1, 4);
	out(0xA0 + 1, 2);

	/* send ICW4 */
	out(0x20 + 1, 0x01);
	out(0xA0 + 1, 0x01);

	/* disable all IRQs */
	out(0x20 + 1, 0xFF);
}
to my video.c source:

Code: Select all

//***********************
// Function: Clear the Screen
// Equivalent: cls();
//***********************

void cls(int color) 
{
  char *vga = (char *) 0xb8000;
  unsigned int i=0;
  while(i < (80*25*2))
  {
      vga[i]=' ';
      i++;
      vga[i]=color;
      i++;
  }
}

//*****************************
// Function: Print Formatted Strings
// Equivalent: printf();
//*****************************

void printf(unsigned char color, char *string, int x, int y)
{
  char *vga = (char *) 0xb8000;
  unsigned int i = 160*y + 2*x;
  while (*string)
  {
	switch(*string)
	{
		case '\n': //newline
		y++;
		i = 160*y + 2*x;
		*string++;
		break;
	}
	//print formatted string
	vga[i++] = *string++;
	vga[i++] = color;
  }
}
My program prints out garbage on the screen instead of the intended message; even without calling the function remap_pics();. When I do call it:

Code: Select all

void main()
{
cls(0x07);
printf(0x07, "Hello",1,1);
remap_pics();
for(;;);
}
The system crashes.

My out() and in() functions are both in my ports.c source:

Code: Select all

unsigned char in(unsigned short _port) //input a byte
{
  // "=a" (result) means: put AL register in variable result when finished
  // "d" (_port) means: load EDX with _port
  unsigned char result;
  __asm__ __volatile__("in %%dx, %%al" : "=a" (result) : "d" (_port));
  return result;
}

void out(unsigned short _port, unsigned char _data) //output a byte to a port
{
  // "a" (_data) means: load EAX with _data
  // "d" (_port) means: load EDX with _port
  __asm__ __volatile__("out %%al, %%dx" : :"a" (_data), "d" (_port));
}
Any help?
Last edited by newwen on Mon Feb 04, 2008 4:46 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Are you loading a fixed amount of disk sectors?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
newwen
Posts: 16
Joined: Sun Feb 03, 2008 3:04 am

Post by newwen »

Code: Select all

reset_drive:
        mov ah, 0               ; RESET-command
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

        mov ax, 0
        mov es, ax
        mov bx, 0x1000          ; We load our code at offset: 0:1000h

        mov ah, 02h             ; READ SECTOR-command
        mov al, 02h             ; Number of sectors to read = 1
        mov ch, 0               ; Cylinder = 0
        mov cl, 02h             ; Sector = 2
        mov dh, 0               ; Head = 0
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

does that mean that you load 512 bytes from the floppy when your kernel is by far larger than that? :roll:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
newwen
Posts: 16
Joined: Sun Feb 03, 2008 3:04 am

Post by newwen »

It must. What should I do?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

eeeeeeeeeh, load more sectors than just one?

Seriously, your intelligence level seems to hit an all time low at this point, even though I have the idea that you are far more capable than this. I suggest you read http://catb.org/~esr/faqs/smart-questions.html - it at least helps in not making more of a fool from yourself.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
newwen
Posts: 16
Joined: Sun Feb 03, 2008 3:04 am

Post by newwen »

It was late and I thought you might say something like that.. haha. I should have said How should I do that? So, just add more sectors and that is it?

Code: Select all

reset_drive:
        mov ah, 0               ; RESET-command
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0

        mov ax, 0
        mov es, ax
        mov bx, 0x1000          ; We load our code at offset: 0:1000h

        mov ah, 02h             ; READ SECTOR-command
mov al, 04h ; Number of sectors to read = 3

Code: Select all

        mov ch, 0               ; Cylinder = 0
        mov cl, 02h             ; Sector = 2
        mov dh, 0               ; Head = 0
        int 13h                 ; Call interrupt 13h
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0 
[edit]OK, that worked, so how many would you recommend I read in? My goal is to eventually read one file from floppy? Is it smart to read in the whole disk?[/edit]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

*string++; -> *(string++);

I had a problem like that, and the above fixed it.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

I don't know why you need to dereference 'string' to increment the pointer value anyway...

Cheers,
Adam
Post Reply