OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Tue Oct 16, 2018 7:15 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
thanks for the tips!for the nasm tip (-g -F dwarf), it says: 'unrecognized debug format 'dwarf' for output format 'bin'' even if its elf32, and for the qemu options i cant even boot, and i don't understand at all what it's happening, but anyway i still having those 'S' or '=S' (the '=' was the character with one more line) and it doesn't print any rtc value as it should be

Thanks!


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Tue Oct 16, 2018 7:31 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Put the -g -F dwarf after the -f elf

As for those QEMU commands did you try putting them as is into a shell file? Did you make any mods to the commands I gave?

My recommendation for using GDB and debugging is so that you can find out yourself why things are failing rather than rely on others to do it. Getting debugging going allows you to step through the code, display variables,set breakpoints, etc.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Tue Oct 16, 2018 8:51 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
Getting back to the problems at hand: let's start with the function get_rtc(), which maybe should be named print_rtc(), but whatever.

Code:
/* Entry Function */
int get_rtc(void)
{
    while(isupdateinprogress());
    uint8 hour = bcd_to_int(get_RTC_val(HOUR));
    print(hour);
}


Now, for reference, let me post the definitions for the string type:

Code:
typedef char* string;


(... I will leave it to others to pontificate on just how advisable this typedef might or might not be...)

and the print() function:
Code:
void print (string ch)
{
        uint16 i = 0;
        uint8 length = strlength(ch)-1;              //Updated (Now we store string length on a variable to call the function only once)
        for(i;i<length;i++)
        {
                printch(ch[i]);
        }
}


See the problem, yet? If not, here's a simpler example which should make it clear:
Code:
#include <stdio.h>

int foo(void)
{
  return 23;
}

void bar(char *baz)
{
  printf("%s", baz);
}

void foo_bar()
{
    bar(foo());
}


It should become especially obvious if you try running GCC with the -Wall option, and I will bet you get a whole bunch of warnings like these:

Code:
> gcc -Wall -c implconv.c
implconv.c: In function ‘foo_bar’:
implconv.c:15:9: warning: passing argument 1 of ‘bar’ makes pointer from integer without a cast [-Wint-conversion]
     bar(foo());
         ^~~~~
implconv.c:8:16: note: expected ‘char *’ but argument is of type ‘int’
void bar(char *baz)
          ~~~~~~^~~


In other words, at least part of the problem is that you are passing a one-byte unsigned integer to a function which is expecting a char pointer (32 or 64 bit, depending, though the size mismatch is the least of the problems).

As thomtl already said, the solution is to convert the integer value into a string one, though the function thomtl mentioned, itoa() is a) non-standard b) deprecated, and c) wouldn't be part of your own library anyway, as you haven't written it yet.

You will definitely need to write something similar eventually, though.

However... in this case, it may be easier to skip the integer conversion, and simply convert the BCD values - after all, this was always one of the putative advantages of using BCD (there were others, but most of them no longer really apply to modern systems, and the ones which do, are often poorly supported). So, if we assume (uh oh, assuming things...) that you are using ASCII or an ASCII-compatible encoding such as UTF-8 or Latin-1, you could add something like this to the bcd.h and bcd.c files:

bcd.h
Code:
#define BCD_ASCII_OFFSETS 0x30


bcd.c
Code:
string bcd_into_str(bcd_t bcd, string buf, unsigned long offset)
{
   unsigned long len;
   char* ptr;

   len = strlen(buf);

   if (len < 2 || len < offset)
   {
      return NULL;
   }
   else
   {
      ptr = buf + offset;

      // hex 30 == ASCII '0'
      *ptr = (char) (bcd >> 4) + 0x30;
      *(++ptr) = (char) (bcd & 0x0F) + 0x30;
   }
   
   return buf;
}


Note that it places the two characters somewhere in the string; it is up to the programmer using it to make sure the string is big enough, delimited, etc. Caveat programmer.

Then for the function returning the RTC time, you would use something like this:
Code:
/* get_rtc_time() - get the time from the real-time clock.
Value is given as a packed string; */
void get_rtc_time(string buf, uint32_t h_offset, uint32_t m_offset, uint32_t s_offset)
{
    while(isupdateinprogress());
    bcd_to_str(get_RTC_val(HOUR), buf, h_offset);
    bcd_to_str(get_RTC_val(MINUTE), buf, m_offset);
    bcd_to_str(get_RTC_val(SECOND), buf, s_offset);
}


You would use it something like this (not tested, I can't promise it will work right):

Code:
    char time[9];

    strncpy("00:00:00", time, 9);

    get_rtc_time(time, 0, 3, 6);


Which should fill in those "00"s with the hour, minute, and second values into the string time.

While this peculiar setup? Because you might want to have different time string formats, and this will make that a little easier in some ways (and trickier in others). C programming is like that sometimes.

Note that you will need to implement your own strlen(), or at least borrow one from (for example) PDCLib, for this to work. OS programming is like that.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Wed Oct 17, 2018 4:46 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
it still not working. now i got the same '=S' but with a space before it. i've changed from:
Code:
void print(string ch)
{
    uint16 i = 0;
    uint8 length = strlength(ch) - 1;
    for(i;i<length;i++)
    {
        printch(ch[i]);
    }
}


to:
Code:
void print(uint8 *ch)
{
    uint8 length = strlength(ch) - 1;
    for(uint16 i = 0;i<length;i++)
    {
        printch(ch[i]);
    }
}


but as i said, still not working. it happens the same with the methods posted before(in page 1 and 2)

Thanks!


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Wed Oct 17, 2018 4:55 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
alberinfo wrote:
but as i said, still not working. it happens the same with the methods posted before(in page 1 and 2)
.Can you update your github to the most recent code you believe isn't working.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Wed Oct 17, 2018 4:59 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
i've updated it


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Wed Oct 17, 2018 5:06 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
alberinfo wrote:
it still not working. now i got the same '=S' but with a space before it. i've changed [...] to:
Code:
void print(uint8 *ch)
{
    uint8 length = strlength(ch) - 1;
    for(uint16 i = 0;i<length;i++)
    {
        printch(ch[i]);
    }
}



I am somewhat speechless. You're now treating uint8_t value as a uint8_t pointer. And then expecting that this single byte containing an integer value will behave like a 32-bit pointer to a zero-delimited array of ... uint8_t?

I think you need to step away from this for a moment, and collect your thoughts, as you seem to be thrashing. It happens to the best of us, but it does mean that you need to step off for a bit and think more carefully about it.

You might need to review the topics of pointers, arrays, arrays to pointers, and arrays to zero-delimited arrays of characters. Even if you do understand them already, you are acting as if you don't understand them, which is why I think you are thrashing about.

I don't want to assume that this is what is happening, but I thought I would say this to give you a chance to look at the possibility.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Wed Oct 17, 2018 5:33 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
i fixed it. the problem wasn't the print function, i had to convert it to a str with the code provided by @School-R-LEA but changing the 'string buf' to 'uint8 buf'

thanks!


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Thu Oct 18, 2018 11:02 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
I think you may find that this solution is flawed, and I can explain why if you like, but I will leave it to you for now.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Thu Oct 18, 2018 1:47 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
well, you're actually right; the only function that works is the hour, and the minute and second work bad.the second gives an 'S', and the minute repeats the hour, puts an 'n' and then the minute.

Edit: and please, explain to me, 'cause i dont understand why it fails


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Thu Oct 18, 2018 5:00 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
alberinfo wrote:
please, explain to me, 'cause i dont understand why it fails


OK, but I may need a bit more information about what you are doing, where certain things are any how you are using them, and how much you know about certain topics (especially regarding C).

To start with, where in your OS code is the strlength() used in print()?

EDIT: nevermind, i found it; for some reason, the GitHub search tool didn't look in include/acpi/string.c, apparently, or perhaps I needed to run the search differently. My Bad. The code is:

Code:
uint16 strlength(string ch)
{
        uint16 i = 1;
        while(ch[i++]); 
        return --i;
}


Anyway, now that I've found that, it looks like a fairly standard implementation of strlen(), as I expected (if one excludes the off-by-one error in it). I would like it if you could explain how you think it works, though.

(And why you have both this and a correct implementation of strlen() in the same file...

Code:
size_t strlen(const char *_s)
{
   size_t n;
   for(n=0; _s[n] != '\0'; n++) {
      ;
   }
   return n;
}


Feel free to explain this, too, as well as how and why the two are different. I am definitely interesting in the answers.)

Also, what does this comment in string.h about the strEql() function refer to?

Code:
/* Added in episode 3*/


The comment seems to imply that at least some of this was gleaned from an video tutorial (I have a guess as to which one, but I'm not sure). Could you tell us which one, preferably with a link to it? That will help us get on the same page, I hope.

Also, how well do you know C strings, and specifically, what they actually are and how they work? Have you worked with C arrays before? Have you studied C pointers, and used them before in a non-trivial manner (e.g., not just passing them to existing functions)? Do you understand the difference between a string literal, a pointer to a string literal, an array, and a pointer to an array? The problems are all tied up in this topic, and I want to get a better feel for what you know (and what you think you know) before I formulate my explanation.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Thu Oct 18, 2018 5:59 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
hi,
my knowledge about programming is not much, and are acquired by my own, 'cause i'm only twelve years old (no, i'm not trolling, and i do not know how did i get here) .note that if i do not understand something i search for it, but i try to understand the code in front of me as far as i can, and then modify it to use it in other function and even other projects.i think that the laguage that i worked the most is Visual Basic from visual studio, but, that does not apply to OS development, does it ?, also i've read about C programming (also with c ++ java and uml from "luis joyanes aguilar" (yeah , i'm spanish) from mcgraw hill editorial education, but i havent reached them yet, and another book about Objective-C, from "fernando lopez hernandez", alfaomega editorial), but almost the first book is under windows libraries

the comment comes from: [url] https://www.youtube.com/watch?v=rr-9w2g ... 7UkQP_KLC3 [/ url]. i started like 5 or 6 months ago with this tutorial and it's files, but now i'm planting if i should write my own libraries.

about strings, as far as i know, they are characters together resulting in a string.i have not worked with arrays, but i understand them.i have 'studied' or better said learned that an pointer points to a memory address, but in the practice sometimes acts like a pointer, and sometimes it does not.i do not want to disappoint you, but no, i dont know the difference between a string literal an pointer to string literal etc, but i can deduce that one is the string itself, and the other says where is the string in memory is it

Thanks!


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Fri Oct 19, 2018 9:42 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
OK, this gives me a place to start.

I won't restart the debate over under-13 y.o. members, but I expect this may lead one of the mods to act. It isn't any fault of yours, but there are some legal considerations involved, though only really relevant to the U.S. so they probably don't apply here.

I will say that it sounds as if - in terms of experience with programming - you are jumping the gun on this. OS dev is not a project for novices; it is a challenge for even the most experienced systems programmers. I am not saying you shouldn't go forward with the project, but you might want to shelve it for a while and build up your expertise before coming back to it. That's up to you, though.

I have a 'boilerplate' set of links for pages in the wiki which I recommend to all new members, so I will start by posting that. I'll get to the topics of C arrays and strings shortly, but I may need to head out soon so that will come in a later post.

The wiki pages I recommend reading, before going any further in the project are the following. I don't know which, if any, you have already read, but re-reading them may be worthwhile anyway.

Getting Started
How To Ask Questions
FAQ (you need read everything listed there, but you would do well to look at the category itself to see the range of common questions)

Required Knowledge
Beginner Mistakes
What order should I make things in
Code Management

How kernel, compiler, and C library work together
Using Programming Languages other than C

Why do I need a Cross Compiler?
GCC Cross-Compiler (if you are planning to use GCC)
OS Specific Toolchain (again, only if you are using GCC and binutils)

To this, I would add that if you are focusing on an x86 system, you should read through these as well (again, at minimum, skim all the way through them at least once, ahead of time):

Real Mode, especially the section on memory addressing, and Segmentation
Memory Map, Detecting Memory and A20 Line
BIOS, and Boot Sequence
Interrupts
Bootloader and Rolling Your Own Bootloader
FAT and SFS

While this is a lot of reading, it simply reflects the due diligence that any OS-devver needs to go through in order to get anywhere. OS development, even as a simple project, is not amenable to the Stack Overflow cut-and-paste model of software development; you really need to understand a fair amount of the concepts and principles before writing any code, and the examples given in tutorials and forum posts generally are exactly that. Copying an existing code snippet without at least a basic idea of what it is doing simply won't do. While learning itself is an iterative process - you learn one thing, try it out, see what worked and what didn't, read some more, etc. - in this case a basic foundation is needed at the start. Without a solid understanding of at least some of the core ideas before starting, you simply can't get very far in OS dev.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: print correctly BCD CMOS RTC values
PostPosted: Fri Oct 19, 2018 1:08 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
thanks for the links!much of them i have readed yet, but anyway theres happens nothing on re-reading them.also i've fixed the problem, the offset for the hour the minute and the second should be '0'.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 87 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