OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:43 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Function to get a character at the current cursor position
PostPosted: Sun Dec 19, 2021 11:14 am 
Offline
Member
Member

Joined: Mon Nov 15, 2021 9:48 pm
Posts: 79
Hi, I'm trying to make a function to get a character at a cursor position. So far, I have:

Code:
char getCharacterAtCursor() {

    unsigned short *index;
    index = &videoMemory + ((cursorY * 80) + cursorX);
    return *index;
}


Thanks!


Top
 Profile  
 
 Post subject: Re: Function to get a character at the current cursor positi
PostPosted: Sun Dec 19, 2021 11:37 am 
Offline
Member
Member

Joined: Sun Aug 20, 2017 10:59 am
Posts: 43
What is 'videoMemory' ? I don't understand why you are using &videoMemory...
Be careful when computing addresses inside of pointers, and go back to computing the address as a void* / integer directly if you have a doubt.
Anyway, in the end you will want an uint8_t/char pointer so doing the calculations in an uint16_t pointer is not really necessary.
This should work : (VGA_MEM_ADDR is vga mem start address, e.g. 0xB8000, and VGA_COLUMNS is 80 ; addr_t is uint32_t or uint64_t depending on architecture)

Code:
char getCharacterAtCursor()
{
    addr_t address = VGA_MEM_ADDR + (cursorY * VGA_COLUMNS * 2) + (cursorX * 2);
    return *((char*) address);
}


Top
 Profile  
 
 Post subject: Re: Function to get a character at the current cursor positi
PostPosted: Sun Dec 19, 2021 11:55 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
Code:
char getCharacterAtCursor(void) {
  struct {
    uint8_t character;
    uint8_t attribute;
  } (*screen)[height][width] = (void*)videoMem;
  return (*screen)[cursorY][cursorX].character;
}


With constant width and height, this works with C89, and with variable width and height this requires C99. You are writing in a high-level language, you don't have to write assembler. In this case, the compiler is able and willing to do the address calculation for you.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Octocontrabass and 76 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group