UnmappedStack / SpecOS

An x86_64 OS kernel from scratch.
MIT License
161 stars 4 forks source link

rtc.c code improvement #2

Closed 0l1ve1r4 closed 3 months ago

0l1ve1r4 commented 3 months ago

Hey bro. I was looking in your code (to get references while i develops mine), and i noticed you can make improvements in your rtc to not redo the same code. my improvements were:

static char* unit_converter(uint8_t s1, uint8_t s2, uint8_t s3, char delimiter) {
    static char to_return[9];
    uint8_t units[] = {s1, s2, s3};

    for (int i = 0, j = 0; i < 3; ++i) {
        to_return[j++] = '0' + units[i] / 10;
        to_return[j++] = '0' + units[i] % 10;
        if (i < 2) {
            to_return[j++] = delimiter;
        }
    }
    to_return[8] = '\0';
    return to_return;
}

const char* get_date(void) {
    return unit_converter(get_time_unit(0x07), 
            get_time_unit(0x08), 
            get_time_unit(0x09), '/');
}

const char* get_time(void) {
    return unit_converter(get_time_unit(0x04), 
            get_time_unit(0x02), 
            get_time_unit(0x00), ':');
}

Also, you need to import in the header.

#include <stdint.h>

/* Get the current date */
const char* get_date(void);

/* Get the current time in UTC */
const char* get_time(void);
UnmappedStack commented 3 months ago

Thanks, I'll have a look at those. Also, is not needed in the header, as it only uses native integer types in the header.

Btw I really don't recommend using my code as a reference, I've stated in the readme that it's really not well written lol.

0l1ve1r4 commented 3 months ago

Thanks, I'll have a look at those. Also, is not needed in the header, as it only uses native integer types in the header.

My bad, I just put them because I like to specify the size of the variable when I'm dealing with functions that will interact with the hardware, but anyway, they're the same thing.

Btw I really don't recommend using my code as a reference, I've stated in the readme that it's really not well written lol.

Yeah, I had read about it, but when it came to the rct, its function was straight to the point when interacting with the time/clock chips.

UnmappedStack commented 3 months ago

Yeah, I had read about it, but when it came to the rct, its function was straight to the point when interacting with the time/clock chips.

Fair, but seriously don't do it with the FAT driver, that's the most messy part of the project lol. The RTC isn't horrible, but def not amazing.

Yeah I use the , , and headers almost everywhere else, but I (mostly) don't use it where it isn't needed.