x49gp/timer.c
claudiol c61f11cff2 On behalf of: 3298 - Applied 23 patches by 3298:
Misc changes, mostly fixes:
- fix ./newconfig systems other than OSX (broke in c8b823f)
- fix palette usage in 2-bit color mode (was broken ever since grayscale was implemented in 18e1003 and its improperly attributed copy f7913eb)
- fix continuing from breakpoints in the debugger (never worked, was exposed when the debugger was enabled in 9c1f2ed)
- restore the printf statements commented out in 9c1f2ed and hide them with #ifdefs instead
- close the server socket after accepting a debugger connection to allow another simultaneous debug session to be started using the same TCP port
- use the symbolic constant DEFAULT_GDBSTUB_PORT (already defined in gdb_stub.h as 1234) when starting the gdb server in main.c in place of the raw number 1234
- change Makefile to read the name of the firmware file from the file update.scp instead of hardcoding it; this allows users to switch to another firmware
by simply pasting it along with its accompanying update.scp into the x49gp directory

- Enhance port G (keyboard) handling to remember the value of output bits across periods with these bits configured as input
This fixes interaction with HPGCC3 keyboard routines, and it also fixes keys with eint==7 (assuming the stock firmware is in use)
needing a double-tap to work unless pressed very shortly after another keypress (the latter broke in b5f93ed)
- Get rid of the deprecated function warning by switching from gdk_pixbuf_new_from_inline to gdk_pixbuf_new_from_data (based on code by chwdt)
- Delete remaining now-redundant CVS files
- Don't release all buttons anymore if there are still physical keys and/or the left mouse button are holding some down
On the other hand, forcibly release all buttons when losing focus to avoid getting stuck in a state with buttons down
when they are not held down by anything; this would happen due to missed events while not in focus
- Add a context menu to the screen, containing only "Reset" and "Quit" items for now
- Ensure that the files backing flash, sram, and s3c2410-sram exist and have the correct size when opening them
Note that if the flash file does not exist, this will not fill it with the code that's supposed to be in there, obviously causing the calculator to crash. That's an improvement for later.
- Allow the config system to fill not only numbers, but also strings (including filenames) with default values
basename is excluded, but it's planned to be dropped entirely.
- Add an "install" target to the Makefile
- Implement a more generic command-line parser for substantially improved flexibility
- Also adds a proper help option, though the manual referenced in the corresponding output (a manpage, hopefully) does not exist yet.
- Drop the "basename" config property in favor of interpreting relative paths in the config as relative to the config file's location
- Retire the "image" config property in favor of simply loading the image from next to the binary or from the install directory
- Split the UI name property into name (affecting only the window title) and type (affecting the UI image and in the future also the default bootcode) properties
- Change the default calculator type to the 50g everywhere, which probably matches today's user expectations better than the 49g+.
- Create a flash file from the calculator model's appropriate boot file if it does not exist, relying on the bootcode to detect the absence of a firmware
  The bootcode will complain about the missing firmware and enter update mode, so the user needs to supply their favorite firmware version and point the bootcode's updater to it.
  The easiest way is probably pointing the emulated SD card at a directory containing the firmware and its accompanying update.scp file, and then starting the SD-based update.
- Add SD mount / unmount options to the right-click / menu-key popup menu
- Remove most of the old script-based config-generating system since the binary now has these capabilities as well
- Add an applications menu item for installing
- Keep some debug output on stderr and a huge vvfat.log file from showing up when not debugging x49gp itself
- Allow (re-)connecting a debugger to a running session
  This is done through the right-click / menu-key popup menu.
  To avoid confusion due to the accidental clicks leading to an unresponsive interface (caused by waiting for the debugger to connect),
  this option is hidden unless option -d or its new companion -D (same as -d, but does not start the debug interface right away) is present.
- Improved support for hardware keyboards
- Update README.md, add manpage, rename other README files to TODO to reflect their contents
2018-05-07 17:32:14 -04:00

329 lines
6.1 KiB
C

/* $Id: timer.c,v 1.4 2008/12/11 12:18:17 ecd Exp $
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <limits.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <poll.h>
#include <x49gp.h>
#include <x49gp_timer.h>
#include <s3c2410.h>
#include <glib.h>
#include "gdbstub.h"
typedef struct {
long type;
} x49gp_clock_t;
struct x49gp_timer_s {
long type;
int64_t expires;
x49gp_timer_cb_t cb;
void *user_data;
x49gp_timer_t *next;
};
typedef x49gp_timer_cb_t QEMUTimerCB;
typedef void * QEMUClock;
QEMUClock *rt_clock = (void *) X49GP_TIMER_REALTIME;
QEMUClock *vm_clock = (void *) X49GP_TIMER_VIRTUAL;
int64_t ticks_per_sec = 1000000;
static x49gp_timer_t *x49gp_timer_lists[2];
int64_t
x49gp_get_clock(void)
{
struct timeval tv;
int64_t us;
gettimeofday(&tv, NULL);
us = tv.tv_sec * 1000000LL + tv.tv_usec;
return us;
}
x49gp_timer_t *
x49gp_new_timer(long type, x49gp_timer_cb_t cb, void *user_data)
{
x49gp_timer_t *ts;
ts = malloc(sizeof(x49gp_timer_t));
if (NULL == ts) {
return NULL;
}
memset(ts, 0, sizeof(x49gp_timer_t));
ts->type = type;
ts->cb = cb;
ts->user_data = user_data;
return ts;
}
void
x49gp_free_timer(x49gp_timer_t *ts)
{
free(ts);
}
void
x49gp_del_timer(x49gp_timer_t *ts)
{
x49gp_timer_t **pt, *t;
// printf("%s: ts %p\n", __FUNCTION__, ts);
pt = &x49gp_timer_lists[ts->type];
while (1) {
t = *pt;
if (NULL == t)
break;
if (t == ts) {
*pt = t->next;
ts->next = NULL;
break;
}
pt = &t->next;
}
}
void
x49gp_mod_timer(x49gp_timer_t *ts, int64_t expires)
{
x49gp_timer_t **pt, *t;
x49gp_del_timer(ts);
// printf("%s: ts %p, expires %lld\n", __FUNCTION__, ts, expires);
pt = &x49gp_timer_lists[ts->type];
while (1) {
t = *pt;
if (NULL == t)
break;
if (t->expires > expires)
break;
pt = &t->next;
}
ts->expires = expires;
ts->next = *pt;
*pt = ts;
}
int
x49gp_timer_pending(x49gp_timer_t *ts)
{
x49gp_timer_t *t;
for (t = x49gp_timer_lists[ts->type]; t; t = t->next) {
if (t == ts)
return 1;
}
return 0;
}
int64_t
x49gp_timer_expires(x49gp_timer_t *ts)
{
return ts->expires;
}
static int
x49gp_timer_expired(x49gp_timer_t *timer_head, int64_t current_time)
{
if (NULL == timer_head)
return 0;
return (timer_head->expires <= current_time);
}
#ifndef QEMU_OLD // LD TEMPO HACK
QEMUTimer *
qemu_new_timer(QEMUClock *clock, QEMUTimerCB cb, void *opaque)
{
return (void *) x49gp_new_timer((long) clock, cb, opaque);
}
void
qemu_free_timer(QEMUTimer *ts)
{
return x49gp_free_timer((void *) ts);
}
void
qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
{
return x49gp_mod_timer((void *) ts, expire_time);
}
void
qemu_del_timer(QEMUTimer *ts)
{
return x49gp_del_timer((void *) ts);
}
int
qemu_timer_pending(QEMUTimer *ts)
{
return x49gp_timer_pending((void *) ts);
}
int64_t
qemu_get_clock(QEMUClock *clock)
{
return x49gp_get_clock();
}
#endif /* QEMU_OLD */
static void
x49gp_run_timers(x49gp_timer_t **ptimer_head, int64_t current_time)
{
x49gp_timer_t *ts;
// printf("%s: now %lld\n", __FUNCTION__, current_time);
while (1) {
ts = *ptimer_head;
if (NULL == ts || ts->expires > current_time)
break;
*ptimer_head = ts->next;
ts->next = NULL;
// printf("%s: call ts %p\n", __FUNCTION__, ts);
ts->cb(ts->user_data);
// printf("%s: ts %p done\n", __FUNCTION__, ts);
}
// printf("%s: timers done\n", __FUNCTION__);
}
static void
x49gp_alarm_handler(int sig)
{
if (x49gp_timer_expired(x49gp_timer_lists[X49GP_TIMER_VIRTUAL],
x49gp_get_clock()) ||
x49gp_timer_expired(x49gp_timer_lists[X49GP_TIMER_REALTIME],
x49gp_get_clock())) {
#ifdef QEMU_OLD
if (cpu_single_env && ! (cpu_single_env->interrupt_request & CPU_INTERRUPT_EXIT)) {
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
}
#else
if (cpu_single_env && ! cpu_single_env->exit_request) {
cpu_exit(cpu_single_env);
}
#endif
}
}
static void
x49gp_main_loop_wait(x49gp_t *x49gp, int timeout)
{
// printf("%s: timeout: %d\n", __FUNCTION__, timeout);
if (gdb_poll(x49gp->env)) {
gdb_handlesig(x49gp->env, 0);
} else
poll(NULL, 0, timeout);
if (x49gp->arm_idle != X49GP_ARM_OFF) {
x49gp_run_timers(&x49gp_timer_lists[X49GP_TIMER_VIRTUAL],
x49gp_get_clock());
}
x49gp_run_timers(&x49gp_timer_lists[X49GP_TIMER_REALTIME],
x49gp_get_clock());
// printf("%s: done\n", __FUNCTION__);
}
int
x49gp_main_loop(x49gp_t *x49gp)
{
int prev_idle;
int ret, timeout;
while (! x49gp->arm_exit) {
prev_idle = x49gp->arm_idle;
if (x49gp->arm_idle == X49GP_ARM_RUN) {
#ifdef DEBUG_X49GP_TIMER_IDLE
printf("%lld: %s: call cpu_exec(%p)\n", (unsigned long long) x49gp_get_clock(), __FUNCTION__, x49gp->env);
#endif
ret = cpu_exec(x49gp->env);
#ifdef DEBUG_X49GP_TIMER_IDLE
printf("%lld: %s: cpu_exec(): %d, PC %08x\n", (unsigned long long) x49gp_get_clock(), __FUNCTION__, ret, x49gp->env->regs[15]);
#endif
if (x49gp->env->regs[15] == 0x8620) {
printf("PC %08x: SRAM %08x: %08x %08x %08x <%08x>\n", x49gp->env->regs[15], 0x08000a0c,
* ((uint32_t *) &x49gp->sram[0x0a00]),
* ((uint32_t *) &x49gp->sram[0x0a04]),
* ((uint32_t *) &x49gp->sram[0x0a08]),
* ((uint32_t *) &x49gp->sram[0x0a0c]));
* ((uint32_t *) &x49gp->sram[0x0a0c]) = 0x00000000;
}
if (ret == EXCP_DEBUG) {
gdb_handlesig(x49gp->env, SIGTRAP);
continue;
}
if (x49gp->arm_idle != prev_idle) {
if (x49gp->arm_idle == X49GP_ARM_OFF) {
x49gp_lcd_update(x49gp);
cpu_reset(x49gp->env);
}
}
if (ret == EXCP_HALTED) {
timeout = 10;
} else {
timeout = 0;
}
} else {
timeout = 1;
}
x49gp_main_loop_wait(x49gp, timeout);
}
return 0;
}
int
x49gp_timer_init(x49gp_t *x49gp)
{
struct sigaction sa;
struct itimerval it;
x49gp_timer_lists[X49GP_TIMER_VIRTUAL] = NULL;
x49gp_timer_lists[X49GP_TIMER_REALTIME] = NULL;
sigfillset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = x49gp_alarm_handler;
sigaction(SIGALRM, &sa, NULL);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000;
it.it_value.tv_sec = 0;
it.it_value.tv_usec = 1000;
setitimer(ITIMER_REAL, &it, NULL);
return 0;
}