PPCenter :: devblog

PPcenter. Arts and craft for my Sega Saturn. Since 1847 :D

Bit shift in Saturn minimalistic C program

Written by cafealpha 4 comments
While making a Saturn program running directly from ROM, constraint is usually to have program as small as possible. Why ? Because I would like to make Pseudo Saturn Kai continue supporting Action Replay, which have limited ROM size

So, in that situation, I use homemade, minimalistic stdlib, containing usually only memcpy and memset functions, and everything goes well.
But for some reason, current program was begging for __ashlsi3 and __ashrsi3 functions.
... What's these things ? __ashlsi3 is an assembly function doing left shifts of data, and as you probably guessed, __ashrsi3 does the same, but with right shifts

No problem, let's ask my friend google, and copy & paste theses functions without minding about their contents Theses functions are standard functions provided by C library, so sources can be found here and there.

An example of what __ashrsi3 function looks like

A day after that, here comes the time to do basic testing of this ROM code
Let's first start with simple things such as screen display ...

Text display ... not as expected ?!

Hrm, it's probably a problem in my printchar routine ?

printchar source code : this is the core thing
behind text display in my programs

But that's a very long time I didn't touched this routine, which works well on Pseudo Saturn Kai, and works even better in vdp1ex from Charles MacDonald sample programs, because all my printchar routines come from there
But just in case of, let's try to tune one " <<3 " into " *8 " in the code above, and see if that changes something. Theses two operations are equivalent, so this should give the same result, but ... Saturn screen becomes all black

Okay, there's definitely something weird regarding that printchar, but I just re-used it as-is from other project, so what's going wrong with my it ?!
And then (finally) a light-bulb above my head appears Maybe there's something wrong regarding the __ashlsi3 and __ashrsi3 routines ?
And, yes there was something wrong regarding theses routines ... I wrote a paragraph about them on the first half of this article, so it would be surprising the problem came from somewhere else

In more accurate words, routines are OK, which is normal since they are written by people smarter than me But the calling convention of __ashrsi3 was not as my gcc was expecting ?
Well, that's just a guess from the display results, because it seems to display the same pixel 8 times on each row of a given character, which may happen when ignoring shift parameter in __ashrsi3 function.

Anyway, rather than finding in details what's wrong, let's fix the problem First, with assembly by hand, in desperately trying to change input parameter handling ... I don't remember exactly what I wrote, but that was something like pushing a register to stack, moving parameter to it, finally restoring register, putting nop instructions everywhere, etc ... nothing difficult, but over my extent in assembly programming

The result is ... a mixed success :

Trying to modify __ashrsi3 routine by hand ... well, it seems that
some shift values are not correctly handled

Okay, so let's re-ask google about that __ashrsi3 routine ... and a different implementation arrives in search results. Let's try it


And the execution result :

Execution result with second version of __ashrsi3 function

Yeah, it works this time !!! And don't ask me why, because I didn't took time in understanding what's different in that second version of __ashrsi3 function
And, rather than understanding why it works ... let's close that assembly pandora box before some other mystic bugs pop from it

I will probably see about this in the future, but that was absolutely out of the scope of today's programming, and in software world, too much digression leads to freeze of projects, which I would like to avoid in order to go forward to next pending project 

As a example, I started to adapt yeti3d engine to Saturn in 2010 (7 years ago !), and this ended in ... developing a SD card based memory cartridge for Saturn !
The point above is is not a joke : menus used in my yeti3d adaptation are the origin of menus used in Pseudo Saturn Kai, which shows the continuity (co-consanguinity ? ) in my projects. And after getting yeti3d working a bit, I really would like to load levels from something else than CD-ROM, which was from PC (via adequate link cable) on a first time, and which will (should ?) then evolve to SD card.
That would be cool to make a Saturn game not requiring to burn CD-ROM somedays, but before that I need to finish neighboring side quests

Evolution of my Saturn projects

To conclude this article, let's say it was terribly fun to see some bits of the internals behind C language
By the past, I remember I did something similar with my yeti3d adaptation, but remained at "C language level" : optimization done at that time was (IIRC, ) to avoid 4 bits shifts, because theses are not available in a single SH-2 CPU instruction. Avoiding 4 bits shifts was simply done by merging two fixed point operation in a single one (or something like that : I did this 7 years ago !), and this actually gave some improvements in 3D scene rendering Theses were the good times ... I sometimes think "I'm Getting Too Old For This Shit", but that's only to motivate to finish my old projects

But before that, Pseudo Saturn Kai, and then Kicad and Quartus are waiting for me

BTW, in the case you wondered about what was today's programming session, you probably guessed it was about exception handler addition to Pseudo Saturn Kai. This not a new feature, since it was available in Pseudo Saturn 0.83x ... in fact, I grabbed some sources from Pseudo Saturn 0.83x in order to implement this exception handler (thank you CyberWarriorX !) This exception handler will be available as a small add-don to cheat codes feature in Pseudo Saturn Kai next major release.

Read more Bit shift in Saturn minimalistic C program

Saturn cartridge firmware flasher utility : major (final ?) update

Written by cafealpha 6 comments
I made many fixes to my flasher utility. (Old article available here , even older article available here)

Changelog :
  - Tested on around 30 Action Replay cartridges. It was working fine for all known flash chips.
  - Ask for confirmation before returning to multiplayer screen.
  - Return back to initial screen in the case user don't want to go to multiplayer screen.
  - Retrieve and display chips IDs when firmware selection menu is displaying.
  - Fixed erase/flash operations for AT29C010 flash chips.
  - Minor fix about text display.
  - Fix for BFBF/1010 flash chips.
  - In the case Action Replay flash chips is supected, display disclaimer
    before flashing as a typical Action Replay flash device (SST29EE).
  - Refresh vendor/device IDs display during error/disclaimer message display
    in the case flash is not recognized.
  - Major changes in menu related routines.
  - Added key auto repeat feature : holding Up/Down button should scroll automatically now.
  - Added erase verification test.
  - If erase/write verification test failed, display informations about failure.
  - Use FRT-based delay during flash write/erase.
  - Changed highlight color in menus.
  - Change color of vendor/device strings according to chips flashability.
     - Hopeless   : red
     - Maybe OK   : yellow
     - Recognized : green

In short, it now has nearly the same compatibility as legacy arflash/Atlas utility, while being SGL-free (it doesn't uses Sega libraries), compatible with newer flash chips, embedding several firmwares in a same executable file, etc, etc.

In the case this flasher is not compatible with your Action Replay (it may happen, I don't decide about future revisions of Action Replay cartridges), please let me know in this article's comments or by email .
However, please note that I don't plan to add new features to this utility. New features were added during beta testing phase, and I really thanks beta testers for this.

Some screenshots from beta testers :
BFBF/1212 flash chip, now recognized as SST/Greenliant type.
Picture provided by gameofyou1.

0101/2020 flash chips : everything is working fine, as in the previous version of the flasher utility.
Picture provided by TabajaraLabs.


Weird 53BF/4707 flash chips : one chip is recognized as typical BF/07 (SST), while the other doesn't returns its vendor/device IDs.
As you can see on PCB picture, flash chips on right side are different, so that they return different vendor/device IDs.
Pictures provided by A Murder Of Crows.



1F1F/D5D5 flash chips : pictures before and after I fix the Flasher utility.
It (finally) works on latest version
Pictures provided by Sonic Dreamcaster.



BFBF/1010 flash chips : according to datasheet, theses flash chips have twice more memory than usual chips (total 256KB -> 512KB).
However, a look at cartridge PCB between C6 capacitor and bottom-right flash chip shows that most significant address bit is grounded, so only 256KB can be addressed from Saturn ... what a pity
Pictures provided by SaturnAR and Zorlon.

You can download Flasher utility and its source code here : flasher_arp_20150326.7zhttp://ppcenter.webou.net/pskai/
Disclaimer #1 : You will loose your cartridge backup data after flashing it.
Disclaimer #2 : I'm not responsible in the case I brick your cartridge and/or damage your Saturn.

Note : I didn't made PseudoSaturn firmware. All thanks about PseudoSaturn firmware must go to CyberwarriorX.

Read more Saturn cartridge firmware flasher utility : major (final ?) update

Rss feed of the tag