Commit Graph

125 Commits

Author SHA1 Message Date
Lephe 74b2dbeb36
string: use __restrict instead of restrict for C++ compatibility 2021-04-27 14:48:34 +02:00
Lephe c37f150600
use GINT_CALL() in every API that has callbacks
* Change gint_inth_callback()
* Add intc_handler_function() to use C functions as handlers instead of
  writing assembler, and use it in the RTC and USB

* Revisit the TMU handlers, which after moving out the callbacks, now
  fit into 3 gates (great!), and adapt the ETMU handler
* Improve the timer driver (less code = better code, removed magic
  constants assuming the VBR layout on SH3/SH4, etc.)
* Remove 2 gates and a gap from the compact scheme on SH3

* Define timer_configure() to replace timer_setup(), which could not be
  cleanly updated to support GINT_CALL()
* Replace rtc_start/stop_timer with rtc_periodic_enable/disable, which
  is less confusing because of ETMU being "RTC timers"
2021-04-27 14:29:38 +02:00
Lephe 85d30fa59b
kernel: specify the return values of standard GINT_CALLs
They return 0 by default, which corresponds to TIMER_CONTINUE when used
in timers. Add new *_STOP variables which return 1 (TIMER_STOP).
2021-04-26 22:01:14 +02:00
Lephe 89cab4f68e
add utility types BFile_FileType, off_t 2021-04-26 21:58:52 +02:00
Lephe c9264a06d5
kernel: driver and world system overhaul
Changes in the driver and world system:

* Rewrite driver logic to include more advanced concepts. The notion of
  binding a driver to a device is introduced to formalize wait(); power
  management is now built-in instead of being handled by the drivers
  (for instance DMA). The new driver model is described in great detail
  in <gint/drivers.h>

* Formalized the concept of "world switch" where the hardware state is
  saved and later restored. As a tool, the world switch turns out to be
  very stable, and allows a lot of hardware manipulation that would be
  edgy at best when running in the OS world.

* Added a GINT_DRV_SHARED flag for drivers to specify that their state
  is shared between worlds and not saved/restored. This has a couple of
  uses.

* Exposed a lot more of the internal driver/world system as their is no
  particular downside to it. This includes stuff in <gint/drivers.h>
  and the driver's state structures in <gint/drivers/states.h>. This is
  useful for debugging and for cracked concepts, but there is no
  API stability guarantee.

* Added a more flexible driver level system that allows any 2-digit
  level to be used.

Feature changes:

* Added a CPU driver that provides the VBR change as its state save.
  Because the whole context switch relied on interrupts being disabled
  anyway, there is no longer an inversion of control when setting the
  VBR; this is just part of the CPU driver's configuration. The CPU
  driver may also support other features such as XYRAM block transfer
  in the future.

* Moved gint_inthandler() to the INTC driver under the name
  intc_handler(), pairing up again with intc_priority().

* Added a reentrant atomic lock based on the test-and-set primitive.
  Interrupts are disabled with IMASK=15 for the duration of atomic
  operations.

* Enabled the DMA driver on SH7305-based fx-9860G. The DMA provides
  little benefit on this platform because the RAM is generally faster
  and buffers are ultimately small. The DMA is still not available on
  SH3-based fx-9860G models.

* Solved an extremely obnoxious bug in timer_spin_wait() where the
  timer is not freed, causing the callback to be called when interrupts
  are re-enabled. This increments a random value on the stack. As a
  consequence of the change, removed the long delays in the USB driver
  since they are not actually needed.

Minor changes:

* Deprecated some of the elements in <gint/hardware.h>. There really is
  no good way to "enumerate" devices yet.

* Deprecated gint_switch() in favor of a new function
  gint_world_switch() which uses the GINT_CALL abstraction.

* Made the fx-9860G VRAM 32-aligned so that it can be used for tests
  with the DMA.

Some features of the driver and world systems have not been implemented
yet, but may be in the future:

* Some driver flags should be per-world in order to create multiple
  gint worlds. This would be useful in Yatis' hypervisor.
* A GINT_DRV_LAZY flag would be useful for drivers that don't want to
  be started up automatically during a world switch. This is relevant
  for drivers that have a slow start/stop sequence. However, this is
  tricky to do correctly as it requires dynamic start/stop and also
  tracking which world the current hardware state belongs to.
2021-04-23 20:44:08 +02:00
Lephe 770b4e0117
gint: rename the callbacks GINT_CALL, as there are synchronous ones
This will be useful for world switch calls which are not "callbacks" but
simply polymorphic function pointers.
2021-04-20 17:51:53 +02:00
Lephe 52bc1fc848
(typos and TODOs) 2021-04-20 12:19:24 +02:00
Lephe 1315c26099
usb: USB 2.0 function driver (WIP)
* Add the power management functions (mostly stable even under
  overclock; requires some testing, but no known issue)

* Add a dynamic configuration system where interfaces can declare
  descriptors with arbitrary endpoint numbers and additional
  parameters, and the driver allocates USB resources (endpoints, pipes
  and FIFO memory) between interfaces at startup. This allows
  implementations of different classes to be independent from each
  other.

* Add responses to common SETUP requests.

* Add pipe logic that allows programs to write data synchronously or
  asynchronously to pipes, in a single or several fragments, regardless
  of the buffer size (still WIP with a few details to polish and the
  API is not public yet).

* Add a WIP bulk IN interface that allows sending data to the host.
  This will eventually support the fxlink protocol.
2021-04-11 19:04:54 +02:00
Lephe a2fd9e3351
kernel: add a generic callback mechanism
This mechanism allows callbacks to be defined with up to 4 32-bit
arguments, and could be extended later. This will hopefully replace the
timer_callback_t used in timers and RTC, and will be added to the DMA
and USB APIs -- the hard part is to not break source compatibility with
previous versions.
2021-04-11 18:47:17 +02:00
Lephe 18f9a18925
kmalloc: implement a custom segregated list allocator
The new allocator uses a segregated best-fit algorithm with exact-size
lists for all sizes between 8 bytes (the minimum) and 60 bytes, one list
for blocks of size 64-252 and one for larger blocks.

Arenas managed by this allocator have built-in statistics that track
used and free memory (accounting for block headers), peak memory, and
various allocation results.

In addition, the allocator has self-checks in the form of integrity
verifications, that can be enabled with -DGINT_KMALLOC_DEBUG=1 at
configuration time or with the :dev configuration for GiteaPC. This is
used by gintctl.

The kmalloc interface is extended with a new arena covering all unused
memory in user RAM, managed by gint's allocator. It spans about 4 kB on
SH3 fx-9860G, 16 kB on SH4 fx-9860G, and 500 kB on fx-CG 50, in addition
to the OS heap. This new arena is now the default arena for malloc(),
except on SH3 where some heap problems are currently known.
2021-03-15 15:04:24 +01:00
Lephe 162b11cc73
kmalloc: create the kmalloc interface
This change introduces a centralized memory allocator in the kernel.
This interface can call into multiple arenas, including the default OS
heap and planned arenas managed by a gint algorithm.

The main advantage of this method is that it allows the heap to be
extended over previously-unused areas of RAM such as the end of the
static RAM region (apart from where the stack resides). Not using the OS
heap is also sometimes a matter of correctness since on some OS versions
the heap is known to fragment badly and degrade over time.

I hope the deep control this interfaces gives over meomry allocation
will allow very particular applications like object-specific allocators
in fragmented SPU memory.

This change does not introduce any new algorithm or arena so programs
should behave exactly as before.
2021-03-12 17:24:49 +01:00
Lephe 910677f7ff
getkey: use the new keydev interface
The repeat filter was also changed in favor of a forward-acting
function, which determines repeat delays *before* the repeat actually
occurs.
2021-03-05 14:49:24 +01:00
Lephe dd564f094a
keydev: add a keyboard device layer to abstract away globals
The new keyboard device (keydev) interface implements the kernel's view
of a keyboard providing input events. Its main role is to abstract all
the globals of the KEYSC driver and getkey functions into a separate
object: the "keyboard device".

The device implements event transformations such as modifiers and
repeats, instead of leaving them to getkey. While this can seem
surprising at first, a real keyboard controller is responsible for
repeats and modifier actions depend on the state of the keyboard which
is only tracked in real-time.

In this commit, getkey() has not changed yet apart from indirectly using
the keydev interface with pollevent(). It will be changed soon to use
event transforms in keydev_read(), and will be left in charge of
providing repeat profiles, handling return-to-menu, backlight changes
and timeouts, all of which are user convenience features.
2021-03-05 09:31:34 +01:00
Lephe 97ca5ba82f
display: add a dfont_default() function to get the default font 2021-02-28 22:28:12 +01:00
Lephe bd40a0a41c
display: add more text size options dnsize() and drsize()
* dnsize() works like dsize() but a limit on the number of bytes is
  specified. This is useful to obtain the length of a substring.
* drsize() has a reverse limit; the input specifies a number of pixels
  and the function determines how much of the input fits. This is useful
  for word wrapping algorithms.
2021-02-22 16:43:02 +01:00
Lephe a086510885
display: add a maximum size parameter to dtext_opt()
This parameter controls the maximum number of glyphs to print.

For backwards compatibility, it is automatically inserted by a macro in
older calls with only 7 parameters.
2021-02-15 18:37:06 +01:00
Lephe cb2d067967
std/string: add strchr(), strrchr(), and strchrnul() 2021-02-15 17:28:11 +01:00
Lephe 553982a445
std/stdio: support asprintf() and vasprintf() 2021-02-15 09:48:10 +01:00
Lephe 2c9ff901d1
mmu: add an mmu_uram_size() function detecting static RAM size
This function performs a more rigorous analysis of the mapped region by
checking continuity. So far all pages mapped in userpsace have been
contiguous, so the results are identical to gint[HWURAM].

Page size is now optionnaly provided in mmu_translate() and its
subfunctions; programs that use this function need to add a second NULL
parameter.
2021-02-05 18:20:16 +01:00
Lephe bbf6401213
kprint: add floating-point formatting based in Grisu2b 59,56 2021-02-02 22:18:15 +01:00
Lephe 023675d449
improve structure of sources and formatted printer
* Create an `src/3rdparty` folder for third-party code (to add the
  Grisu2B alfogithm soon).
* Split the formatted printer into gint's kprint (src/kprint), its
  extension and interface (include/gint/kprint.h), and its use in the
  standard stdio functions (src/std/print.c).
* Slightly improve the interface of kformat_geometry() to avoid relying
  on knowing format specifiers.
* Add a function to register more formatters, to allow floating-point
  formatters without requiring them.
2021-02-02 18:19:10 +01:00
Lephe ee7b4f27b8
add a dynamic setting for keyboard scan frequency
The repeat delays of getkey() are adjusted automatically, however a
repeat that is currently going on might be affected.

Also, repeat delays are always approximated as a whole number of
keyboard scans so an increase in scan frequency can impact the speed at
which repeats are emitted.
2021-02-02 18:19:10 +01:00
Lephe 6440527527
rtc: add an rtc_ticks() function similar to RTC_GetTicks() 2021-02-02 18:19:08 +01:00
Lephe 8157f4b2ab
cmake: restore options USER_VRAM and STATIC_GRAY 2021-01-25 19:06:42 +01:00
Lephe 0525b51ba5
switch build system to CMake
This factors the burden of compiler specification in the fxSDK while
providing substantially more support through CMake modules.
2021-01-25 15:31:20 +01:00
Lephe 7a68070bac
show version in header for static version resolution
This change moves the gint version declaration from a symbol in a
compile-time generated object file to a preprocessed header installed in
the library tree.

This makes it possible to determine the gint version statically from the
headers, which is much more robust in complex build systems that use
version information such as CMake's find_package().
2020-12-30 18:35:06 +01:00
Lephe 3694f20d56
spu: starter driver, supporting direct CPU access to SPU memory 2020-10-21 18:29:04 +02:00
Lephe 8ff7d89d33
cpg, tmu: add spin waiting and spin delay functions for drivers
This change adds a new TMU function timer_spinwait() which waits for a
timer to raise its UNF flag. This makes it possible to wait even when
interrupts are disabled.

This is used by the new CPG function sleep_us_spin() which waits for a
given delay without using interrupts. This is currently used in SPU
initialization.
2020-10-21 14:49:34 +02:00
Lephe 858ec8aa12
cpg, power: improve peripheral register descriptions
The POWER and CPG modules have been reverse-engineered by Yatis.
2020-10-21 14:48:04 +02:00
Lephe 19951ccf62
mmu: add MMU registers with a driver context for PASCR and IRMCR 2020-10-21 14:44:47 +02:00
Lephe bc575f1599
intc: update interrupt sources in IPR registers
As per new documentation reverse-engineered from CPU73050.DLL.
https://bible.planet-casio.com/lephenixnoir/sh7305/intc/interrupt-sources.html
2020-10-09 18:03:08 +02:00
Lephe 52d95e72ed
stdlib: force rand() to return a non-negative number
Negative numbers come with tricky modulus results and are excluded from
the return values of the standard rand().
2020-10-09 09:17:48 +02:00
Lephe 078edb50b2
small cleanup 2020-10-05 16:31:37 +02:00
Lephe 240f29f9d5
topti: custom character and word spacing (#13)
This commit introduces custom character spacing with a new fxconv
parameter "char-spacing". Word spacing is also tied to the width of the
space character (0x20). This removes the need for special semantics on
the space character, but requires that its size be specified with gray
pixels for proportional fonts.

This also fixes problems with the size of spaces in dsize() not being
correlated with their size during rendering, since on fx-9860G topti
already used the glyph's with as word spacing.

Since fxconv changes but gint's Makefile does not track updates to
external tools, a full rebuild of gint is required past this commit.
2020-10-05 16:14:12 +02:00
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 d12be8add0
remove features that are deprecated as of v2.1 2020-07-26 11:47:23 +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 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 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 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 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