Commit Graph

222 Commits

Author SHA1 Message Date
Lephe 589c25c10c
kernel: do not rely on COMMON sections for GMAPPED refs
Seems to break with binutils 2.35.
2020-09-22 15:17:31 +02:00
Lephe e5abe03b89
kernel: dynamic loading of GMAPPED functions to user RAM
This commit introduces a large architectural change. Unlike previous
models of the fx-9860G series, the G-III models have a new user RAM
address different from 8801c000. The purpose of this change is to
dynamically load GMAPPED functions to this address by querying the TLB,
and call them through a function pointer whose address is determined
when loading.

Because of the overhead of using a function pointer in both assembly and
C code, changes have been made to avoid GMAPPED functions altogether.
Current, only cpu_setVBR() and gint_inth_callback() are left, the second
being used specifically to enable TLB misses when needed.

* Add a .gint.mappedrel section for the function pointers holding
  addresses to GMAPPED functions; add function pointers for
  cpu_setVBR() and gint_inth_callback()
* Move rram to address 0 instead of the hardcoded 0x8801c000
* Load GMAPPED functions at their linked address + the physical address
  user RAM is mapped, to and compute their function pointers
* Remove the GMAPPED macro since no user function needs it anymore
* Add section flags "ax" (code) or "aw" (data) to every custom .section
  in assembler code, as they default to unpredictable values that can
  cause the section to be marked NOLOAD by the linker
* Update the main kernel, TMU, ETMU and RTC interrupt handlers to use
  the new indirect calling method

This is made possible by new MMU functions giving direct access to the
physical area behind any virtualized page.

* Add an mmu_translate() function to query the TLB
* Add an mmu_uram() function to access user RAM from P1

The exception catching mechanism has been modified to avoid the use of
GMAPPED functions altogether.

* Set SR.BL=0 and SR.IMASK=15 before calling exception catchers
* Move gint_exc_skip() to normal text ROM
* Also fix registers not being popped off the stack before a panic

The timer drivers have also been modified to avoid GMAPPED functions.

* Invoke timer_stop() through gint_inth_callback() and move it to ROM
* Move and expand the ETMU driver to span 3 blocks at 0xd00 (ETMU4)
* Remove the timer_clear() function by inlining it into the ETMU handler
  (TCR is provided within the storage block of each timer)
* Also split src/timer/inth.s into src/timer/inth-{tmu,etmu}.s

Additionally, VBR addresses are now determined at runtime to further
reduce hardcoded memory layout addresses in the linker script.

* Determine fx-9860G VBR addresses dynamically from mmu_uram()
* Determine fx-CG 50 VBR addresses dynamically from mmu_uram()
* Remove linker symbols for VBR addresses

Comments and documentation have been updated throughout the code to
reflect the changes.
2020-09-17 14:48:54 +02:00
Lephe 4288dc27d9
keyboard: add custom repeat filters for full repeat control
This change introduces a new getkey_repeat_filter() function that can be
used to individually accept, deny or delay repeat events for specific
keys and timings.
2020-08-05 11:50:32 +02:00
Lephe a4d23ef7ad
libc: faster memset 2020-07-28 17:34:25 +02:00
Lephe 492f61f7b2
libc: faster memcpy for on-chip memory
An optimization suggested by TSWilliamson, which pushes not only RAM,
but also on-chip memory and the CPU pipeline to their limits.
2020-07-27 22:47:30 +02:00
Lephe 7b4eb078c4
move src/core to src/kernel 2020-07-26 11:49:33 +02:00
Lephe d12be8add0
remove features that are deprecated as of v2.1 2020-07-26 11:47:23 +02:00
Lephe d887423bbb
bopti: more performance improvement for SCSP cases
* Turn on GCC's -O3 for bopti files
* Remove the bopti_render_noclip() step
* Use rbox as early as possible to avoid moving memory around
* A lot of local grinding
2020-07-23 14:03:45 +02:00
Lephe 11dd04243f
bopti: performance improvements for SCSP cases
* Defined the single-column single-position (SCSP) situation where a
  single column of the input is blit on a single position of the VRAM.
  Provided optimized assembly and a specialized bopti_render_scsp()
  function.
* Improved the rendered by reducing the amount of computation and
  clarifying the semantics of the rbox.
* Separated rbox setup from clipping by making bopti_render_clip() a
  purely abstract superset of bopti_render_noclip().
2020-07-23 10:18:46 +02:00
Lephe 39664e9bd2
bopti: fix bouding bugs in bopti_render_noclip()
These errors ironically made the noclip version slower than its
counterpart.
2020-07-21 08:54:34 +02:00
Lephe 4cb1555708
kernel: add an add-in restart mechanism using gint_osmenu()
This new mechanism allows an add-in to be restarted after exiting by
just never exiting in the first place, calling gint_osmenu() instead.
This makes sure that we can relaunch the add-in immediately, which is
normally possible through an option in the OS though no OS-independent
method of setting it is currently known.

Because this is gint_osmenu(), known pitfalls apply. On all platforms,
it is necessary to prepare the first frame before leaving. On fx-CG 50,
the inevitable display border is also there.
2020-07-20 20:37:34 +02:00
Lephe 6c535bf7df
cpg: add spread spectrum control
Disables spread spectrum by default so that the frequency estimations of
the CPG driver (notably used by the timer driver and libprof) are more
accurate.
2020-07-20 17:10:47 +02:00
Lephe bf21246f13
kernel: mask interrupts during callbacks on fx-CG Manager
The fx-CG Manager holds but ignores the CPUOPM.INTMU bit, which means
that we have to mask interrupts as on SH3.
2020-07-20 16:44:27 +02:00
Lephe 0622928f22
rtc: robust interrupt handling and SH3 compatibility
This commit changes the interrupt handler arrangement to support the PRI
interrupt on SH3 (a gap is needed between 0xaa0 and its helper).

It also introduces the use of the _gint_inth_callback function for the
callback, which provides dynamic TLB during the interrupt, and revealed
a bug about IMASK not being set automatically on SH3.

Finally, it sets the interrupt settings of the RTC more conservatively,
by wiping RCR1 and the carry, alarm and periodic interrupt flags during
initialization and context restoration.
2020-07-19 20:06:50 +02:00
Lephe a06213ca11
kernel: mask interrupts during callbacks on SH3!
This change fixes very weird bugs first observed with the RTC, related
to IMASK not being updated when an interrupt occurs to avoid
re-interruption.

On SH4 there is a CPUOPM setting that automatically sets IMASK to the
level of the accepted interrupt, which is so exactly what every kernel
needs that I can't figure out why this isn't the only behavior.

Turns out on SH3 it's not even an option. This commit sets IMASK to 15
when accepting a callback on SH3. This most notably prevents the gray
engine from updating the screen so callbacks need to be made very short.
2020-07-19 20:02:50 +02:00
Lephe e617ea63bf
keyboard: add keycode functions to identify F1..F6 and 0..9 2020-07-16 17:29:12 +02:00
Lephe 5cac2cf7fc
rtc: take input similar to timer_setup() in rtc_timer_start() 2020-07-16 17:27:56 +02:00
Lephe e16f3acfa1
topti: support Unicode fonts
This change adds UTF-8 decoding to dtext() to fully use Unicode fonts
provided by fxconv.
2020-07-14 15:28:46 +02:00
Lephe e3042755d2
topti: support Unicode in the data structures (WIP)
This change modifies the font_t type to replace the concept of charset
with a more generic list of non-overlapping Unicode blocks defined by a
starting code point and a size.

It also takes advantage of the assembly feature of fxconv, introduced
for libimg long after the first version of topti, to support pointers in
the converted structure rather than having to tediously compute offsets
within a variable-size structure.
2020-07-14 12:11:12 +02:00
Lephe 77de9e7796
move private headers to subfolders of src/
This keeps the include/ folder clean.
2020-07-13 17:47:21 +02:00
Lephe 0eb58c39a3
gray: add the DGRAY_PUSH_ON/OFF and DGRAY_POP feature
DGRAY_PUSH_ON/OFF will push the current gray engine state to a stack
before transitioning to on/off mode. DGRAY_POP will later recover the
saved state and transition back to it.
2020-07-13 17:14:58 +02:00
Lephe 94fb300e72
gray: finalize the gray engine API
* Define dgray() to replace gray_start() and gray_stop()
* Introduce a mechanism to override the d*() functions rather than using
  another set of functions, namely g*(). Gray rendering should now be
  done with d*() (a compatibility macro for g*() is available until v2.1).
* Gray engine now reserves TMU0 at the start of the add-in to prevent
  surprises if timers are exhausted, so it nevers fails to start
* Replace other gray engine functions with dgray_*()
* More general rendering functions (in render/) to lessen the burden of
  porting them to the gray engine. As a consequence, dtext_opt(),
  dprint_opt() and drect_border() are now available in the gray engine,
  which was an omission from 230b796.
* Allow C_NONE in more functions, mainly on fx-CG 50
* Remove the now-unused dupdate_noint()
2020-07-13 13:49:07 +02:00
Lephe 411bbb9568
gray: use 923, 1742 as the default gray settings
Found by Kbd2.
2020-07-10 17:30:00 +02:00
Lephe 2751dcf045
remove the .gint.data section
Since both platforms now have their VBR and gint-specific data loaded
along the add-in's data, the .gint.data section is entirely unused.

The .gint.bss section is still used for uninitialized objects (it has
different semantics than .bss which is initially cleared) and the
.gint.data.sh3 and .gint.bss.sh3 sections that are dropped on the
SH4-only fx-CG 50 are also still used.
2020-07-10 16:36:05 +02:00
Lephe ece65927f0
use less static RAM to improve SH3 support 2020-07-10 16:06:33 +02:00
Lephe b2f580a009
kernel: move VBR to user RAM on fx9860g
This change puts an end to the illegal occupation of some random RAM
area, solving most of the return-to-menu issues, including (but not
limited to) random key presses, multiple return, and VBR being
overwritten.

There are still known return-to-menu issues on SH3 (mainly just
GetKeyWait() returning immediately to gint probably due to multiple
KEY_CTRL_MENU being injected), but nothing crashy.
2020-07-10 16:06:28 +02:00
Lephe 0aceb6f93e
kernel: compact VBR even more on SH3
This change moves interrupt handler from VBR + 0x640 to VBR + 0x200, in
the gap between the exception and TLB miss handlers.

This new scheme is not limited to VBR+0x200 .. VBR+0x400 as new large
block numbers can be used to jump over the TLB miss handler and the
interrupt handler entry points.
2020-07-10 13:07:12 +02:00
Lephe 2b1f408cb4
kernel: compact VBR scheme on SH3
I have recenty discovered that the so-called "rram" section used by gint
to store its VBR space and a couple memory structures gets overwritten
when returning to the main menu. It is thus necessary to get rid of it
and store that data somewhere else.

My current lead is to have it at the start of the static RAM by querying
its address in the TLB. However, the static RAM is very small on SH3
(8k) so the VBR must be made more compact.

This change elaborates the event code translation scheme used on SH3 to
emulate SH4 event codes. It is now used to translate the event codes to
a gint-specific VBR layout that leaves no gaps and thus reduces the size
of the VBR space. The gint_inthandler() method has to be modified for
every new SH3 interrupt to maintain this scheme.
2020-07-09 10:44:37 +02:00
Lephe a99bffe7f4
iokbd keysc: space optimizations for SH3
* Reduce the keyboard queue size from 64 to 32, which is more than
  enough even for real-time games with multiple key presses.
* Pack the driver_event_t structure of the keyboard driver to make it 4
  bytes rather than 6 bytes. Combined with the previous item, this saves
  256 bytes off the BSS section (which is 3% of the SH3's static RAM).
* As part of a debugging attempt, updated the watchdog delay code in
  iokbd_delay() to make it usable in the current version of gint.
* Restored port registers more aggressively in iokbd_row().
2020-07-08 20:01:58 +02:00
Lephe 03715344a4
tmu: safer restore order
The previous setup made it possible for TCNT to underflow to an
uncontroller value of TCOR during the restore.
2020-07-08 19:49:09 +02:00
Lephe e2886d2bd7
kernel: fix trying to set CPUOPM on SH3
Results in an Illegal Instruction no the ICBI that follows the value
change.
2020-07-04 15:16:39 +02:00
Lephe 9d1187b5b4
string: optimized memcpy, memcmp, memset; decent memmove
This change adds optimized versions of the core memory functions,
relying on 4-alignment, 2-alignment, and the SH4's unaligned move
instruction to (hopefully) attain good performance in all situations.
2020-07-04 15:05:28 +02:00
Lephe 7d63a1b536
r61524 render-cg: support Prizm and fx-CG Manager
This change adds a new HWCALC model, HWCALC_FXCG_MANAGER, which
identifies Casio's official fx-CG Manager software. Both the Prizm and,
to my surprise, the fx-CG Manager use the old RAM address of 88000000
(P1) and a8000000 (P2) instead of the new fx-CG 50 address of 8c000000
(P1) and ac000000 (P2).

The VRAM is hence adjusted at startup to move hardcoded pointers into
the proper space. Added to the kernel moving the VBR space dynamically
on the Prizm, this allows gint to be fully compatible with these
platforms.

The fx-CG Manager is detected by its product ID made of 0xff.

Also adds a proper interface to the R61524 driver, even though it's not
any more complete than previously, and fixes an oversight where the
HWURAM entry of the kernel data array was no longer computed since the
TLB management change.

As of now, the fx-CG Manager still has a bug regarding return-to-menu
since returning from the main menu doesn't work very well and often
loops. This has been seen occasionally on some Graph 90+E so it's
unlikely to be a platform-specific problem.
2020-07-02 15:48:19 +02:00
Lephe ed30b2cb21
render-cg: allow VRAM in user stack
This is currently unused and has some artifacts in gintctl for reasons I
haven't understood yet. Will have to investigate later.
2020-07-02 15:47:11 +02:00
Lephe f8ee9b7414
timer: allow integer pointers in callback arguments 2020-07-02 08:51:59 +02:00
Lephe dc83d5ee1f
timer: final iteration on the API
This commit minimally changes the signature of timer_setup() to greatly
simplify timer management, allowing to user to let the library choose
available timers dynamically depending on the settings.
2020-06-20 22:45:46 +02:00
Lephe a91a0a483b
core: remove some useless hardware info 2020-06-20 19:23:50 +02:00
Lephe 4a0fc65c34
gray: add decent defaults for older fx9860g-like models
Gray quality is better on the Graph 35+E II, it still flickers a lot on
other models (as I remembered). There might be better settings out there
but I'm not sure we can reach the quality of the current Graph 35+E II
defaults. The Graph 75+E with which I tested might also be different
from other T6K11 such as the smaller Graph 35+E.
2020-06-20 19:16:42 +02:00
Lephe 1c7b1350b4
general cleanup of the kernel
* Removed .pretext sections since the TLB is now entirely dynamic; left
  only .text.entry for the start symbol.
* Reworked the main files of src/core to move the INTC to its own driver
  and let the kernel handle only VBR space and CPU (now: VBR & CPUOPM).
* Moved src/core/gint.c to src/core/kernel.c and centralized all driver
  loops that save or restore context for more robustness. This leaves
  the callbacks of cpu_setVBR() (formerly gint_setvbr()) pretty short.
* Coalesced gint_switch_out() and gint_switch_in() into a single
  function callback for cpu_setVBR().
* Added an abstraction of interrupt signals as an enumerated value so
  that drivers no longer hardcode the IPR and IMR numbers and bits,
  sometimes even with isSH3() due to differences in the SH7705.
* Changed the interrupt blocking method in cpu_setVBR() from SR.BL=1 to
  SR.IMASK=15 so that TLB misses are still handled. This removes the
  need for callback functions to be GMAPPED.
* Moved gint_osmenu() and its utilities to a new file src/core/osmenu.c.
2020-06-20 17:18:51 +02:00
Lephe b7de559b78
some cleanup in src/core and in the repository 2020-06-20 11:41:13 +02:00
Lephe de4881244e
core: remove bootlog
An unused logging mechanism that was never really fit for its task of
diagnosing boot issues. Disappears with cleanup...
2020-06-20 09:46:39 +02:00
Lephe 9cf2f9fe97
core tmu: improve reliability of interrupt handlers
This change includes three reliability improvements in handlers:

1. TMU handlers now actively check for the UNF flag to go low rather
   than expecting it to do so right away.
2. CPUOPM.INTMU is now set so that IMASK it updated at every interrupt
   (which is absolutely required for nested interrupts!).
3. gint_inth_callback() no longer performs transfers between user bank
   and kernel bank while in user bank, because this is when interrupts
   are enabled and thus likely to corrupt the kernel bank; rather, it
   now does it while in kernel bank with interrupts disabled.
2020-06-20 09:32:48 +02:00
Lephe 91fd2e5e6a
core: fix erroneous VBR address choice in gint_switch()
This one never worked even by accident, it just went untested for a
couple hours.
2020-06-18 22:12:08 +02:00
Lephe bf2d407f1d
tmu: fix dreadful omission of timer ID in ETMU handler
This change fixes a never-should-have-worked problem where the ETMU
interrupt handler loses track of the timer ID before attempting to call
timer_stop(), resulting in complete nonsense.

And also a similar problem in timer_wait().
2020-06-18 20:20:55 +02:00
Lephe 230b796196
render: add alignment options to dtext_opt() and dprint_opt()
This change introduces two new functions dtext_opt() and dprint_opt()
that have both color and alignment options. The regular dtext() and
dprint() have been changed to always used bg=C_NONE which is what most
calls want.
2020-06-18 18:31:13 +02:00
Lephe 06345967fd
render: add a drect_border() function 2020-06-18 17:50:27 +02:00
Lephe 4a3c396284
fxcg50: remove rram region and fxcg20-friendly VBR address
This change removes the RRAM region which was inherited from the fx9860g
memory layout but no longer relevant on fxcg50. This removed one
occurrence of a hardcoded user stack address in the linker script, the
other being the VBR address. But since the VBR only contains
position-independent code that is manually "relocated" at startup, the
linker script needs not actually use its value, so this is not a true
dependency.

gint should now more or less be able to boot up on an fxcg20, except for
the hardcoded VRAM addresses which need to be moved to the fxcg20 system
stack.
2020-06-18 09:59:31 +02:00
Lephe 2fd4238d31
core: finalize TLB management in timer callbacks (STABLE)
This change enables interrupts within timer callbacks, making it
possible to load pages to MMU while handling a timer underflow. The call
to TLB_LoadPTEH() has been moved directly into the VBR handler to avoid
jumping to ILRAM for a short call on SH4.

The TMU and ETMU handlers have been changed to callback through a new
function gint_inth_callback() that saves the user bank and a few
registers, then invokes the callback with interrupts enabled and in user
bank; until now, callbacks were invoked with interrupts disabled and in
kernel bank. Note that IMASK is still set so a callback can only be
interrupted by a high-priority interrupt.

A timer_wait() function has also been added to simplify tests that
involve timers. Finally, the priority level of the TMU0 underflow
interrupt has been set to 13 (as per the comments) instead of 7.

This version is the first stable version that handles TLB misses
transparently for large add-ins. It is suitable for every gint
application.
2020-06-17 11:43:26 +02:00
Lephe 8148d89c88
core: backport TLB handling to fx9860g, fix return-to-menu (UNSTABLE)
This change ports the TLB management system to fx9860g through %003.
This raises the size limit for add-ins to about 500k.

Because SH3 fx9860g does not have ILRAM, the GMAPPED attribute has been
made to generate content to a .gint.mapped section which is sent to the
P1 RAM section historically dubbed "real ram" in which gint's data and
VBR are installed. (Now that I think about it, gint's data should try to
go to normal RAM instead to reduce pressure on this invasion.)

Return-to-menu was also fixed on both platforms by narrowing down the
need for code to remain mapped to the chance of running it with
interrupts disabled. The natural distribution of GMAPPED under this
criterion showed that _gint_setvbr had been left under TLB control;
moving it to the proper RAM area fixed gint switches.

Finally, an omission in the bound checks for mappable TEA addresses (TEA
>= 0x00300000) prevented the appearance of a non-interactible System
ERROR popup when some unmapped addresses are accessed.

This version still does not enable interrupts in timer callbacks,
exposing any application to a crash if a timer underflows while its
callback is not mapped. It is not suitable for any stable application!
2020-06-15 20:55:18 +02:00
Lephe fbfcdd7664
render-fx: fix VRAM overflows in gint_dhline() and gint_dvline()
Similar to 610362f.
2020-06-15 13:45:50 +02:00