Function triple faults PC

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
Hillbillie

Function triple faults PC

Post by Hillbillie »

For some reason I can't figure out, this function resets my
computer when called:

// Displays 'string' at (col, row) in the current attribute.
// Does not modify cursor position.
// Returns nothing.
void Put_String_At(unsigned short int col, unsigned short int row, unsigned char *string)
{
unsigned char ch;
unsigned short int cursor = ((row - 1) * 80 + (col - 1)) * 2;

while(ch = *string++)
{
switch(ch)
{
// Newline.
case '\n':
// Put our cursor at the first character on next line.
// But for now, just print '\n'.
VIDEO[cursor] = '\\';
cursor = cursor + 1;
VIDEO[cursor] = Get_Text_Color();
cursor = cursor + 1;
VIDEO[cursor] = 'n';
cursor = cursor + 1;
VIDEO[cursor] = Get_Text_Color();
cursor = cursor + 1;
break;

// Normal character.
default:
VIDEO[cursor] = ch;
cursor = cursor + 1;
VIDEO[cursor] = Get_Text_Color();
cursor = cursor + 1;
break;
}
}
}

I've looked at it a million times and can't figure out what is wrong.
Can someone please help me. BTW, VIDEO is a pointer to the video memory
(0xb8000) and I'm using DJGPP.
Guest

Another thing...

Post by Guest »

Also, (1, 1) would be the upper left of the screen, not (0, 0).
J. Weeks

RE:Function triple faults PC

Post by J. Weeks »

> unsigned short int cursor = ((row - 1) * 80 + (col - 1)) * 2;

this shouldn't cause a triple fault, but the above
line should actually be:

cursor = (row - 1) * 160 + (col - 1) * 2;
Remember, there's 160 bytes to a line, not 80.

> while(ch = *string++)

Is there a possibility the string isn't \0 terminated?

> VIDEO[cursor] = Get_Text_Color();

If Get_Text_Color(); does something more than
a "return colour;" then the triple fault may
actually be caused by that function... although,
I doubt it.

>I've looked at it a million times and can't figure out what is wrong.
>Can someone please help me. BTW, VIDEO is a pointer to the video memory
>(0xb8000) and I'm using DJGPP.

Positive? Have you enabled paging? Perhaps the
video buffer isn't in your page table?

Or, perhaps, you haven't entered pmode correctly...
is this the very first thing you do after entering
pmode? 'cuz I've often tried to do something
directly after entering pmode which has failed, and
I've blamed the function, when in actuality it was
my pmode code... the processor wasn't properly
put into pmode.

Just a couple suggestions... I really can't
see anything wrong with your code, except the first
line (but then, I'm really tired, so I may
have missed something).

j.weeks
J. Weeks

RE:Function triple faults PC

Post by J. Weeks »

> unsigned short int cursor = ((row - 1) * 80 + (col - 1)) * 2;

this shouldn't cause a triple fault, but the above
line should actually be:

cursor = (row - 1) * 160 + (col - 1) * 2;
Remember, there's 160 bytes to a line, not 80.

> while(ch = *string++)

Is there a possibility the string isn't \0 terminated?

> VIDEO[cursor] = Get_Text_Color();

If Get_Text_Color(); does something more than
a "return colour;" then the triple fault may
actually be caused by that function... although,
I doubt it.

>I've looked at it a million times and can't figure out what is wrong.
>Can someone please help me. BTW, VIDEO is a pointer to the video memory
>(0xb8000) and I'm using DJGPP.

Positive? Have you enabled paging? Perhaps the
video buffer isn't in your page table?

Or, perhaps, you haven't entered pmode correctly...
is this the very first thing you do after entering
pmode? 'cuz I've often tried to do something
directly after entering pmode which has failed, and
I've blamed the function, when in actuality it was
my pmode code... the processor wasn't properly
put into pmode.

Just a couple suggestions... I really can't
see anything wrong with your code, except the first
line (but then, I'm really tired, so I may
have missed something).

j.weeks
Hillbillie

RE:Function triple faults PC

Post by Hillbillie »

>this shouldn't cause a triple fault, but the above
>line should actually be:
>
>cursor = (row - 1) * 160 + (col - 1) * 2;
>Remember, there's 160 bytes to a line, not 80.

I use that same offset algorithm in a Put_Char() function and it works beautifully so it's correct :-)


>Is there a possibility the string isn't \0 terminated?

Well, I always use the function like 'Put_String_At(1, 1, "Hello, World!");' or something similar.


>If Get_Text_Color(); does something more than
>a "return colour;" then the triple fault may
>actually be caused by that function... although,
>I doubt it.

No, it just returns (foreground | background);


>Positive? Have you enabled paging? Perhaps the
>video buffer isn't in your page table?

I have other video output functions and they all work fine. This is the only one that doesn't.

Any other suggestions?
Guest

RE:Function triple faults PC

Post by Guest »

>On 2001-08-11 17:48:05, Hillbillie wrote:

>Can someone please help me. BTW, VIDEO is a pointer to the video memory
>(0xb8000) and I'm using DJGPP.

#include <sys/nearptr.h>
#include <crt0.h>
void main(void) {
unsigned char *VIDEO = (unsigned char *)
0xB8000 + __djgpp_conventional_base;

if(!(_crt0_startup_flags & _CRT0_FLAG_NEARPTR)) {
if(!__djgpp_nearptr_enable()) {
printf("Could not enable nearptr\n");
return -1;
}
}
*VIDEO = '@';
}
Hillbillie

RE:Function triple faults PC

Post by Hillbillie »

>#include <sys/nearptr.h>
>#include <crt0.h>
>void main(void) {
>unsigned char *VIDEO = (unsigned char *)
> 0xB8000 + __djgpp_conventional_base;
>
>if(!(_crt0_startup_flags & _CRT0_FLAG_NEARPTR)) {
> if(!__djgpp_nearptr_enable()) {
> printf("Could not enable nearptr\n");
> return -1;
> }
>}
>*VIDEO = '@';
>}

Hmm...how will this help? Doesn't printf() call the operating system?
Hillbillie

Fixed it!

Post by Hillbillie »

In case someone was having the same trouble, I'll post the updated code and how I fixed it.

// Displays 'string' at (col, row) in the current attribute.
// Does not modify cursor position.
// Returns 0 on success, -1 on error.
signed short int Put_String_At(unsigned short int col, unsigned short int row, unsigned char string[])
{
unsigned short int ch = 0;
unsigned short int offset;

// If coordinates are legit, print that sucker.
if((col < 81) && (col > 0) && (row < 26) && (row > 0))
{
offset = ((row - 1) * 80 + (col - 1)) * 2;

while(string[ch] != 0)
{
// Newline.
if(string[ch] == '\n')
{
// Put our cursor at the first character on next line.
offset = ((offset / 2 / 80) + 1) * 160;

// If we're offscreen, scroll.
if((offset / 2) > 1999)
{
Scroll_Screen(1, 1, 80, (unsigned short int)SCREEN);
offset = 3840;
}
}

// Normal character.
else
{
VIDEO[offset] = string[ch];
offset = offset + 1;
VIDEO[offset] = Get_Text_Color();
offset = offset + 1;
}

ch++;
}

return 0;
}

return -1;
}

Also, when I compiled it, I used the '-fwritable-strings' option.
Post Reply