Add mdview markdown viewer, reorganize tests/examples and libc layout

- Split tests/ (libc feature tests) and examples/ (real apps); shared
  app.mk in repo root, was examples/example.mk
- libc/io/* split into libc/{conio,env,errno,file,mouse,string,sys,
  time,video}/ — clearer module boundaries
- New examples/mdview/: markdown viewer (Phases 1-5 + light nested
  lists). Headers (H1-H4), HR, ulist/olist/quote with nesting via
  leading spaces, fenced code blocks, inline emphasis (bold/italic/
  underscore/code), wrap/unwrap mode with soft wrap (F2), horizontal
  pan (← →) with '>' truncation indicator
- libc additions: scroll() in conio (ESTEX SCROLL), strlwr/strupr,
  gets() test
- Makefile updates across tests/ for the new shared app.mk path

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-04 22:23:36 +03:00
parent b851e22fa6
commit 737c974400
104 changed files with 2485 additions and 223 deletions
+143
View File
@@ -0,0 +1,143 @@
/*
* posix_time.c — minimal POSIX <time.h> implementation on top of
* getdatetime() (ESTEX SYSTIME $21).
*
* SDCC's z80.lib bundles time/localtime/mktime AND _RtcRead in a
* single time.rel module, so the user can't override _RtcRead from a
* separate object — overriding triggers a "multiple definition"
* linker error. We sidestep that by implementing the whole POSIX time
* API ourselves; the linker then never pulls SDCC's time.rel.
*
* The epoch is Unix (1970-01-01 00:00:00). No timezone support —
* gmtime and localtime are identical. No DST.
*/
#include <stdio.h>
#include <time.h>
static const unsigned char mdays[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
static const char *const dnames[7] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat"
};
static const char *const mnames[12] = {
"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
static int is_leap(unsigned int y)
{
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
/* Days elapsed from 1970-01-01 to (y, 1, 1). */
static unsigned long year_days(unsigned int y)
{
unsigned long d = 0;
for (unsigned int i = 1970; i < y; i++)
d += is_leap(i) ? 366 : 365;
return d;
}
/* Days from Jan 1 to month start (1-based month input). */
static unsigned int month_days(unsigned int y, unsigned int m)
{
unsigned int d = 0;
for (unsigned int i = 0; i < m - 1; i++) d += mdays[i];
if (m > 2 && is_leap(y)) d++;
return d;
}
time_t time(time_t *t)
{
datetime_t dt;
getdatetime(&dt);
unsigned long days = year_days(dt.year)
+ month_days(dt.year, dt.month)
+ (dt.day - 1);
time_t epoch = days * 86400UL
+ (unsigned long)dt.hour * 3600UL
+ (unsigned long)dt.minute * 60UL
+ dt.second;
if (t) *t = epoch;
return epoch;
}
/* localtime and gmtime share one static buffer — caller copies if
* needed across further calls (matches POSIX behaviour). */
static struct tm tm_buf;
struct tm *gmtime(time_t *timep)
{
unsigned long sec = *timep;
tm_buf.tm_sec = (unsigned char)(sec % 60); sec /= 60;
tm_buf.tm_min = (unsigned char)(sec % 60); sec /= 60;
tm_buf.tm_hour = (unsigned char)(sec % 24); sec /= 24;
/* sec is now days since 1970-01-01 (Thursday). */
tm_buf.tm_wday = (unsigned char)((4 + sec) % 7);
/* find year */
unsigned int y = 1970;
unsigned long days = sec;
while (days >= (unsigned long)(is_leap(y) ? 366 : 365)) {
days -= is_leap(y) ? 366 : 365;
y++;
}
tm_buf.tm_year = (int)y - 1900;
tm_buf.tm_yday = (int)days;
/* find month/day */
unsigned int m = 0;
while (m < 12) {
unsigned int dim = mdays[m] + ((m == 1) && is_leap(y) ? 1u : 0u);
if (days < dim) break;
days -= dim;
m++;
}
tm_buf.tm_mon = (unsigned char)m;
tm_buf.tm_mday = (unsigned char)(days + 1);
tm_buf.tm_isdst = 0;
tm_buf.tm_hundredth = 0;
return &tm_buf;
}
struct tm *localtime(time_t *timep)
{
return gmtime(timep); /* no timezone */
}
time_t mktime(struct tm *tm)
{
unsigned int y = (unsigned int)(tm->tm_year + 1900);
unsigned long days = year_days(y)
+ month_days(y, (unsigned int)tm->tm_mon + 1)
+ (unsigned int)(tm->tm_mday - 1);
time_t epoch = days * 86400UL
+ (unsigned long)tm->tm_hour * 3600UL
+ (unsigned long)tm->tm_min * 60UL
+ tm->tm_sec;
/* Backfill wday/yday so callers can inspect them. */
tm->tm_wday = (unsigned char)((4 + days) % 7);
tm->tm_yday = (int)month_days(y, (unsigned int)tm->tm_mon + 1)
+ (tm->tm_mday - 1);
return epoch;
}
/* "Day Mon DD HH:MM:SS YYYY\n" — 25 chars + NUL. */
static char asctime_buf[26];
char *asctime(struct tm *tm)
{
sprintf(asctime_buf, "%s %s %2d %02d:%02d:%02d %d\n",
dnames[tm->tm_wday % 7],
mnames[tm->tm_mon % 12],
tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm->tm_year + 1900);
return asctime_buf;
}
char *ctime(time_t *timep)
{
return asctime(localtime(timep));
}
+30
View File
@@ -0,0 +1,30 @@
/*
* sleep — block for N seconds using the 50 Hz frame interrupt.
*
* Sprinter ISR fires 50 times per second. `halt` parks the CPU until
* the next IRQ, so 50 halts = ~1 second of wall clock. This is the
* same trick solid-c uses in IO.ASM:285.
*
* Note: requires interrupts to be enabled (they are by default — ESTEX
* sets up IM 1 with the frame ISR before our program runs).
*/
#include <unistd.h>
void sleep(unsigned int seconds) __naked
{
(void)seconds;
__asm
inter:
;; HL = seconds on entry (SDCC single int arg).
ld a, h
or a, l
ret Z ; sleep(0) return immediately
ld b, #50 ; 50 halts per second (50 Hz interrupt)
inner:
halt
djnz inner
dec hl
jr inter
__endasm;
}
+93
View File
@@ -0,0 +1,93 @@
/*
* time.c — getdatetime / setdatetime via ESTEX SYSTIME ($21) / SETTIME ($22).
*
* $21 SYSTIME → D=day E=month IX=year H=hour L=min B=sec C=dow
* $22 SETTIME D=day E=month IX=year H=hour L=min B=sec → CF=err / A=errcode
*
* Struct layout (datetime_t, 8 bytes):
* +0 day, +1 month, +2..3 year, +4 hour, +5 min, +6 sec, +7 dow
*
* Both routines write/read *dt directly — no static scratch. The key
* trick in getdatetime is `ex (sp), hl` after RST: HL holds hour:min,
* stack TOS holds dt — one byte swaps them, then we walk the struct
* via HL. setdatetime uses IX as the struct pointer (re-loaded with
* year just before RST since that's what SETTIME expects).
*/
#include <time.h>
#include <errno.h>
void getdatetime(datetime_t *dt) __naked
{
(void)dt;
__asm
push ix ; save caller IX (BIOS clobbers it)
push hl ; stash dt across RST (clobbers HL)
ld c, #0x21 ; ESTEX SYSTIME
rst #0x10
;; Returns: D=day E=month IX=year H=hour L=min B=sec C=dow
;; ex (sp), hl: TOS<->HL. Now HL = dt, TOS = hour:min stash.
ex (sp), hl
ld (hl), d ; +0 day
inc hl
ld (hl), e ; +1 month
inc hl
push ix ; year stack
pop de ; DE = year
ld (hl), e ; +2 year low
inc hl
ld (hl), d ; +3 year high
inc hl
pop de ; D = hour, E = min (from earlier ex(sp))
ld (hl), d ; +4 hour
inc hl
ld (hl), e ; +5 min
inc hl
ld (hl), b ; +6 sec
inc hl
ld (hl), c ; +7 dow
pop ix ; restore caller IX
ret
__endasm;
}
int setdatetime(const datetime_t *dt) __naked
{
(void)dt;
__asm
push ix ; save caller IX
push hl
pop ix ; IX = dt
ld d, (hl) ; +0 D = day
inc hl
ld e, (hl) ; +1 E = month
inc hl
ld c, (hl) ; +2 year low
inc hl
ld b, (hl) ; +3 year high (BC->IX)
inc hl
push bc
ld b, (hl) ; +4 H = hour
inc hl
ld c, (hl) ; +5 L = min (BC->HL)
inc hl
push bc
ld b, (hl) ; +6 B = sec
pop hl
pop ix
ld c, #0x22 ; ESTEX SETTIME
rst #0x10
pop ix ; restore caller IX
jr c, _st_err2
ld de, #0
ret
_st_err2:
call __errno_set
ld de, #-1
ret
__endasm;
}