I still do not understand how ti_read() reads in an app var into a struct. Data reads incorrectly.
Example code:

This is the struct

Code:

struct PokeData{

    uint8_t stats[202*(6 + 2 + 2 + 2)];
    uint8_t evo[202*2];

    uint16_t nameIndex[203];

    char name[1436];
};


I print out the first 6 from stats, but shows:

Code:
255 153 254 255 255 243

On the app var, it should contain:

Code:
0x28 0x2D 0x3C 0x5A 0x37 ...
(they are in hex because I'm too lazy to convert)
I got these because I used convbin on a binary file that begins with these numbers.
CEmu confirmed that the data was transfered correctly, so no corruption. Checked it in the system, its the right digits.

Did I do something wrong? The ti_read code is (sorry about the bad naming, but I know its not part of the problem):


Code:

PokeData pokedata;
ti_var_t pokeData = ti_Open("PokeData", "r");
ti_Read(&pokedata, sizeof(PokeData), 1, pokeData);
for(int i = 0; i < 6; i++){
dbg_printf("%d ", pokedata.stats[i]);
}


The rest of the struct contains vastly different data from the app var, in fact, here is the more of the wrong data:

Code:

255 153 254 255 255 243
255 138 255 109 255 153
255 138 255 255 255 97
254 243 254 109 255 243
255 243 254 143 255 211
254 255 255 255 255 109
254 109 255 153 254 255
254 243 254 211 255 109
202 211 255 138 255 138
254 255 255 97 255 243
255 109 255 243 254 109
255 211 255 109 255 153
255 109 255 138 255 153
254 255 255 243 254 143
255 138 255 109 255 243
202 211 255 138 255 138

//This is generated using the code:
    for(int i = 0; i < 202; i++){ //there's 202 lines in total, but I'm not showing all of them.
        for(int j = 0; j < 6; j++){
            dbg_printf("%u ", pokedata.stats[12*i + j]);
        }
        dbg_printf("\n");
       
    }


There's a lot of 255 and 254s which I know I did not have in my program. I feel like there is a padding issue, but didn't you guys say that there is no padding?
Are you sure that pokeData is a valid file handle? If ti_Open failed for whatever reason, it would return 0 instead of a valid file handle, which would cause ti_Read to do nothing and pokedata to remain uninitialized and filled with garbage data. You should always check that your file handle is non-zero after after calling ti_Open.

A few other things, which people already mentioned in chat but I feel like should be elaborated on:

You don't actually need to read the entire appvar at once at the beginning of your program. You can either use ti_Seek to seek to the address you want each time you want to read something, or use ti_GetDataPtr to get a pointer to PokeData.

You should avoid using magic numbers in your code. It makes it much easier to read and change stuff later. For example, you could use #define NUM_POKEMON 202 and then replace all instances of 202 in your code with NUM_POKEMON.
You could also get rid of the multiplier for the number of stats by declaring a stats struct, e.g.

Code:
struct stats {
    uint8_t hp;
    uint8_t attack;
    uint8_t defense;
    uint8_t sp_attack;
    uint8_t sp_defense;
    uint8_t speed;
    (etc.)
};

and then having an array of that rather than an array of uint8_ts.
If you're using the ti_GetDataPtr method, you could also get rid of the size of name using a flexible array member and just putting char name[];. If you do this, it means you can no longer create the struct, though, only a pointer to it.
Ok so here's what I found: yes, the file exists and can be read.
I did make changes with the magic numbers after Mateo recommended me to read some article.

Also, I followed someone's recommendation to test the thing with other data. I filled an appvar with junk (all hexadecimal):

Code:

283032313233616C6B73646A667361643B6C766E736B616461666A733B6A626C64736B2C736E7264632C616D647A786E63676C3B6572612E76646D2E7361656466

I did a ti_read on the first 10 uint8_t of the appvar. The results were:

Code:

140
48
50
49
50
51
97
108
107
115

The read was successful, as I set the array to 0 values before the read to insure that it was not undefined behavior. This output is consistent with every read.

I tried to create an array with some values, write it into the calculator, then read it in the same fashion, and got the correct results. I used the emulator to check the value of the appvar, they are same as the one I created using the convbin, but its the right data ?!?

what is going on here?

If you are curious, this is how I created my appvars from a bin file, where every byte in the bin file is useful and packed:

Code:

convbin -i "%TEMP_PATH%\PokeData.bin" -o PokeData.8xv -j bin -k 8xv -n PokeData -r


Remember, the emulator data inspector shows the right numbers in both cases.
Can you provide a minimal reproducible example zip file with the appvar/code you are running?
IGNORE THIS POST! LEAVING THIS HERE FOR ARCHIEVE REASONS, IN CASE I NEED IT AGAIN.

SOMETHING IS WORKING AND I DON'T KNOW WHAT. I'M GETTING IT TO WORK! I'LL GIVE UPDATE WHEN I DETERMINE IT DOES WORK.

https://yolomep.github.io/mimum.zip

there's the appvar and bin file. I feel like I did something dumb during upload. I remember having to update my emulator to send appvar files, when I was using an old version, test.8xv never uploaded.
Another thing - I named the appvar test2 but the file name is test. I open with name test2. I do not think this is an issue.

It's version 1.3 of CEmu.
Ok I figured out how to make it work. Mateo, you lied to me about the ti_Close thing, maybe its because I need to open it in "w" mode first, or its because the emulator needs to close. I just open the file in any edit mode (idk about read mode) then close it, reopen in read. Read in, its correct! Bruh. Also, ignore the link in the previous post, I uploaded the wrong file, which has 2 errors. I'm too lazy to commit again.

ez80-clang version:

Code:

clang version 14.0.0 (https://github.com/jacobly0/llvm-project a139def90d26173f771eb1eca797633d1fbb2797)
Target: ez80
Thread model: posix


idk about clibs. It's the one on jacobly's website.

CEmu version:

Code:

CEmu 0.0.0.0
OK I'm back with the same problem. Apparently it reads the correct data if only one of the appvar has been opened. Opening one appvar reads the right data. Openning a second appvar, the appvar reads with all 0s. Opening with all four produces that weird result I mentioned in the first post. I will actually upload the minimum reproduceable example to https://yolomep.github.io/mimum.zip
The appvars are in the EmerData group. Play around with the commented sections and you will see what I mean.
Yolomep wrote:
OK I'm back with the same problem. Apparently it reads the correct data if only one of the appvar has been opened. Opening one appvar reads the right data. Openning a second appvar, the appvar reads with all 0s. Opening with all four produces that weird result I mentioned in the first post. I will actually upload the minimum reproduceable example to https://yolomep.github.io/mimum.zip
The appvars are in the EmerData group. Play around with the commented sections and you will see what I mean.

This does not compile on my machine. You should make a copy of your project folder, get rid of anything not required to reproduce the issue, ensure that the issue still occurs, and then zip the entire project folder, including the makefile.
OK. I will do. But in the meantime, I have some suspicion to what caused the error. I realized my code was using around 50 KB of memory. I am using ti_seek and reading only a bit to see if there is no error.

Also, does something like:


Code:

class Foo {
    int something;
    ...

}
Foo::Foo() : ... {

 }

int main() {
    Foo arr[10];
    arr[0] = Foo();
}


does the last line create a new class and then copies it to somewhere else or is it just assigning arr[0] a pointer to a foo object? After compile, arr becomes a pointer to a location allocated space for 10 pointers right?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
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 vote in polls in this forum

 

Advertisement