Register

Mapping a file to memory

Discuss issues related to the Casio Prizm 3rd party development
Senior Member
Posts: 141
Joined: Wed May 16, 2012 2:50 pm
Location: Portugal
Calculators: Casio fx-CG20

Re: Mapping a file to memory

Postby gbl08ma » Mon May 16, 2016 9:41 pm

SimonLothar wrote:Hint: If you open a G3A in readwrite mode (3) and use 0x1DAA beyond the last block (in this case 0x1DAA returns -2), the G3A vanishes from the main menu!

Do you have any idea why this happens? Does it vanish until the next reboot? If yes, is there any way to bring the g3a back without rebooting? Sorry for all the questions but this is kind of important for the add-in disabler AmazoNKA was talking about, which I could never get to work in a stable way, because of how poorly the OS handles operations on g3a files.

SimonLothar wrote:You are right: The automatic optimization seems to be a serious problem.

Not so bad as having to deal with TLB restore when the add-in is left. It's a part that actually scares me: some years ago, a user at Cemetech was investigating how the MMU, TLB and exception handling worked on the Prizm. He wanted to add support for dynamic libraries on the Prizm (like DLLs on Windows and SOs on Linux), so people could use code under the GPL license in programs with less restrictive licenses (BSD, MIT, etc.), and most importantly, reuse code between add-ins (to waste less storage space). He made a custom exception handler which worked, but the exception handling settings of the CPU must be restored to their original state before returning to the Casio OS, or it will malfunction. Well, he bricked his calculator because of a series of complications that started when he forgot to reset the MMU and TLB settings to their original values. His (unfinished) work is here: https://github.com/AHelper/ld-prizm

That hopefully explains my emphasis on getting memory mapping to work through OS facilities, which would hopefully not self-destroy themselves. Alas, it seems such a thing doesn't exist. But Bfile_GetBlockAddress is already a quite good outcome, which will save people the work of reading the FAT data structures by hand to achieve the same effect.

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Mapping a file to memory

Postby SimonLothar » Thu May 19, 2016 9:17 am

gbl08ma wrote:Do you have any idea why this happens? Does it vanish until the next reboot?
It does not vanish after reboot, because the G3A is corrupt after the following sequence


Code: Select all
         Bfile_StrToName_ncpy( filename, "\\\\fls0\\NOTEVIEW.G3A", 0x10A );
         handle = Bfile_OpenFile( filename, 3, 0 );
         if ( handle >= 0 ){
            for ( j = 0x0; j < 0x2E000; j+=0x1000 ){
               i = Bfile_GetBlockAddress( handle, j, &iresult );
               if ( i < 0 ) break;   
            };
            Bfile_CloseFile( handle );
         };   



The sequence adds zeros to the open G3A until the the last 0x1000 sector is filled-up, if the "no more sectors"-error occurs ( i == -2 ).
This does not happen, if the file is opened with

handle = Bfile_OpenFile( filename, 1, 0 );
I'll be back!

Senior Member
Posts: 141
Joined: Wed May 16, 2012 2:50 pm
Location: Portugal
Calculators: Casio fx-CG20

Re: Mapping a file to memory

Postby gbl08ma » Thu May 19, 2016 9:57 pm

Oh, it's because it damages the file. It's of no use to me, then. However, I wonder if that "extend with zeros" behavior is how the CreateFile function works (and how WriteFile increases the file size)... I highly doubt it (it's likely something that's done using one of the lower-level Fugue FAT functions), but it's something to keep in mind for the future.

I gave up on trying to read the script from flash directly, for now I'll just use a RAM buffer. I moved on to making a nice text editor for the scripts (which can be used with any kind of text file, really, it's just optimized for C syntax). It's sad that we don't know how to make multi-line editing work with the OS syscalls, but I worked around that limitation. The way I'm doing it, I think I have more control over how the text is rendered and how the cursor moves, so the syscalls would not be of much use anyway.

I'm still using EditMBStringCtrl2 and related ones, but instead of relying on the OS to draw the text for me, I'm drawing it myself. Only drawback is that (because I'm too lazy to implement text selection, and my text drawing code is already a big mess), copying or cutting things to the clipboard has to be done like this:

http://s.lowendshare.com/4/1463691110.9 ... pboard.gif

In developing this I found some interesting things, namely that GetKey will only enter Clip mode (setup setting 0x14 == 2) if the OS cursor is visible. If, while cursor is hidden, one sets setup setting 0x14 to 2 manually (because GetKey won't do it, as the cursor is hidden), EditMBStringCtrl2 enters text selection mode correctly, but pressing F1 or F2 will not copy or cut the text to the clipboard. That's why my editor first enters that single-line editing mode and instructs people to press Shift+8 in order to begin the actual clipping process.

However, pasting text works without any "strange" workaround, even with the cursor hidden (like the GIF shows).

Senior Member
Posts: 116
Joined: Mon Mar 02, 2015 10:53 am
Calculators: Casio fx-CG20

Re: Mapping a file to memory

Postby AmazoNKA » Thu May 19, 2016 11:31 pm

I was using a similar approach when developing my chat application over serial port by using edit but not display routines for input because cursor flashing was not working as desired due to me having to do constant serial receives and similar (no getkey as well to avoid blocking)- I haven't looked at that code for a while but I do not remember having to switch cursor flashing on or off as display routine was not involved... I would've never thought about what you discovered about clip functionality - will try to test one day

From my experience not usuing built in display input system call and printing the input instead had an extra step of me having to deal with translating some keys like plus, minus, etc into other printable codes and similar for keys like sin, cos, etc if you choose to accept them, and my own cursor substitute position needed adjustment calculation to account for multibyte etc characters... Let me know if you want me to look anything particular up for that but i feel you probably already figured out some elegant solution for this

Thanks again for working on txt editor (it is even needed in eigenmath for script etc editing) - one extra feature your txt and picoc editor may benefit from would be QR generation to make archiving and sharing of them easy from calculator to mobile, i know it is only one way but could be quite useful sometimes. I'm interested in QR for making simple classwiz and wolfram alpha etc queries too, which was actually inspired by my chat add-in development and having to translate input codes to sometimes other printable character codes

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Mapping a file to memory

Postby SimonLothar » Sun May 22, 2016 11:05 am

gbl08ma wrote:Oh, it's because it damages the file. It's of no use to me, then...
It is not that bad. As I said, if you open the file in readonly mode,

handle = Bfile_OpenFile( filename, 1, 0 );

the file won't be changed.
I'll be back!

Senior Member
Posts: 116
Joined: Mon Mar 02, 2015 10:53 am
Calculators: Casio fx-CG20

Re: Mapping a file to memory

Postby AmazoNKA » Sun Jun 05, 2016 11:27 am

SimonLothar wrote:
AmazoNKA wrote:Picking up on the last point, is it actually possible to force add-in to restart on Prizm please, in other words actually destroy current execution of the add-in without causing system errors but to be able to start the same last executed add in from the beginning, I probably could try long jump etc but legacy system calls seemed to indicate it was possible, or perhaps someone could guide me what flags could be utilised for the same or recommend other workarounds. Thanks in advance
Perhaps you could use APP_EnableRestartFlag (see below). I excluded OS version 2.02 because I did not verify it, yet. Perhaps the exclusion can be omitted. I just checked the function with version 2.00.
Code: Select all
//
short*APP_EnableRestart(){
unsigned int ea;
unsigned int j;
short*pEnableRestartFlag;
  if ( OSVersionAsInt() < 0x02020000 ){
    ea = *(unsigned int*)0x8002007C;
    ea += 0x1e6e*4;
   
    ea = *(unsigned int*)( ea );
    j = *(unsigned char*)( ea + 1 );
    j *= 4;
    j = ( ea + j + 4 ) & 0xFFFFFFFC;
    pEnableRestartFlag = (short*)(*( unsigned int*)( j ) + 8 );
  }else{
    pEnableRestartFlag = 0;
  }
  if ( pEnableRestartFlag ) *pEnableRestartFlag = 1;
  return pEnableRestartFlag;
}

How dangerous is to run this omitting 2.02 exclusion on 2.02 version please - is there a way to verify it please somehow safely first if on 2.02 OS on real hardware? Or vice versa is there a way to update the emulator within FX-CG Manager Plus to 2.02 version somehow please. Thank you very much again

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Mapping a file to memory

Postby SimonLothar » Sun Jun 05, 2016 12:33 pm

AmazoNKA wrote:is there a way to verify it please somehow safely first if on 2.02 OS on real hardware?
Yes. I have to update one of my Prizms to OS 2.02 (It's high time to do that) and have a look. I'm on it.
I'll be back!

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Mapping a file to memory

Postby SimonLothar » Sun Jun 05, 2016 4:34 pm

SimonLothar wrote:
AmazoNKA wrote:is there a way to verify it please somehow safely first if on 2.02 OS on real hardware?
Yes. I have to update one of my Prizms to OS 2.02 (It's high time to do that) and have a look. I'm on it.
The function works with OS 2.02, too. You can change
if ( OSVersionAsInt() < 0x02020000 ){
to
if ( OSVersionAsInt() <= 0x02020000 ){.
I'll be back!

Senior Member
Posts: 116
Joined: Mon Mar 02, 2015 10:53 am
Calculators: Casio fx-CG20

Re: Mapping a file to memory

Postby AmazoNKA » Sun Jun 05, 2016 4:55 pm

Thank you so much for all the updates

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Mapping a file to memory

Postby SimonLothar » Sun Jun 05, 2016 5:17 pm

BTW.: They changed the checksum calculation range with OS 2.02.
<OS 2.02: 0x80020000 - 0x80B5FEAF; 0x80B5FEF0 - 0x80B5FFF7
OS 2.02: 0x80020000 - 0x80B1FFFF; 0x80B5FEF0 - 0x80B5FFF7
I'll be back!

PreviousNext

Return to Casio Prizm SDK

Who is online

Users browsing this forum: No registered users and 1 guest