Accessing Video Memory

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
beyondsociety

Accessing Video Memory

Post by beyondsociety »

I been trying for days to get a basic printf function to work and at the point of insanity, I'm asking for help.

The printf function is attached below. How would I use it?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Accessing Video Memory

Post by Pype.Clicker »

it seems to me that you forgot the "attribute" byte that comes with the video memory ...
drizzt

Re:Accessing Video Memory

Post by drizzt »

right! test_ptr must be an unsigned word *test_ptr.
But why don't you initialize test_ptr to 0xB8000?!
beyondsociety

Re:Accessing Video Memory

Post by beyondsociety »

I don't really know what you mean?
Whatever5k

Re:Accessing Video Memory

Post by Whatever5k »

Have a look at this:
http://www.mega-tokyo.com/os/os-faq-con ... #text_mode
There's an attribute byte indicating the colour of the character to print right after the character itself...

Video memory starts at address 0xB000. That's why drizzt was wondering why you don't use this address, but (0xB000 - 1MB).
Perhaps you use paging and you don't use 1:1 mapping?
Slasher

Re:Accessing Video Memory

Post by Slasher »

Monochrome display modes start at 0xb0000
Color display modes start at 0xb8000
beyondsociety

Re:Accessing Video Memory

Post by beyondsociety »

Code: Select all

Video memory starts at address 0xB000. That's why drizzt was wondering why you don't use this address, but (0xB000 - 1MB). 
Perhaps you use paging and you don't use 1:1 mapping?
I don't have paging set up yet. Whats 1:1 maping?

Code: Select all

If your kernel's base is not 0, then substract the kernel base from the screen pointer's offset.
What are they referring to when they say if your kernel base is not 0? Is determined by the Code and Data segment in the GDT?

If this so, then maybe this is my problem?
drizzt

Re:Accessing Video Memory

Post by drizzt »

I don't have paging set up yet. Whats 1:1 maping?
1:1 mapping means that every virtual address (when paging is setup) corresponds with the same physical address.
What are they referring to when they say if your kernel base is not 0? Is determined by the Code and Data segment in the GDT?

If this so, then maybe this is my problem?
If you want to write something in the screen you must put characters into the video memory buffer. This starts at physical address 0xB8000 if you have a color display, otherwise, if you have a monochrome, at 0xB0000.

So if your data selector (placed into the register ds) is not 0-based you must subtract from the video mem address the base of your selector.
beyondsociety

Re:Accessing Video Memory

Post by beyondsociety »

Code: Select all

So if your data selector (placed into the register ds) is not 0-based you must subtract from the video mem address the base of your selector. 
How would I check for this? Where would I look?
drizzt

Re:Accessing Video Memory

Post by drizzt »

When you setup your GDT you define your selectors. Before switching into pmode you must load a GDT with the "lgdt" instruction.
Then you set the PE bit on (bit 0 of cr0 register) and do a far jump to reload CS selector.
Finally you reload your segment registers with the opportune selectors. This is a short summary about what you have to do to switch into PMode (probably you know this...)

I help you to find the base of your data selector with an example...
drizzt

Re:Accessing Video Memory

Post by drizzt »

This is the structure of every GDT entry:

dw 0 ; limit 15:0
dw 0 ; base 15:0
db 0 ; base 23:16
db 0 ; access byte (descriptor type)
db 0 ; limit 19:16, flags
db 0 ; base 31:24

As you can see you can check the base of your selector simply looking at its definition; for example this is a 0-based selector:

DATA_SEL equ $-gdt ; 0x10
dw 0xFFFF ; Limit/Length
dw 0 ; Base 15:0
db 0 ; Base 23:16
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0 ; Base 31:24

This one has base 0x100000:

DATA_SEL equ $-gdt ; 0x10
dw 0xFFFF ; Limit/Length
dw 0x0000 ; Base 15:0
db 0x10 ; Base 23:16
db 0x92 ; present, ring 0, data, expand-up, writable
db 0xCF ; page-granular, 32-bit
db 0 ; Base 31:24
beyondsociety

Re:Accessing Video Memory

Post by beyondsociety »

Thanks for the help. I got it working. By the way, I decided to write some simpler code as the code posted above was an example from alt-os.development.

I noticed two problems: one, the attribute byte and the pointer accessing the video memory.

Second, I noticed that if I loaded my C kernel to 1MB, it didn't work. But if I loaded a lower address, it worked.

Weird huh? I have the A20 gate enabled. C kernel code attached below.
Whatever5k

Re:Accessing Video Memory

Post by Whatever5k »

How is your kernel loaded, do you use GRUB? How do you link your kernel, and do you have an asm file - please post it...
beyondsociety

Re:Accessing Video Memory

Post by beyondsociety »

I figured it out. I was compiling my code wrong.
Post Reply