diff --git a/docs/progress.md b/docs/progress.md index e91990c..7525514 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -35,7 +35,7 @@ - [x] Contact list: GET_CONTACTS into a Palm database + contacts screen - [x] Messages: store, sync on PUSH_MSG_WAITING, send with ack tracking (built; to verify on the Visor) - [x] MeshTerm-style UI: small 4x6 font, Home (conversations), Conversation, Contacts, Node (built; to verify) -- [ ] Split the code into segments: code resource is 28.3 KB of the ~32 KB single-segment limit +- [x] Split the code into segments: protocol.c + ui/text.c in vmcode2 (seg 1 24.3 KB, seg 2 4.9 KB); rules in src/sections.h (to verify on the Visor) - [ ] UI: contacts, DMs, channels - [x] Install method confirmed: HotSync over the USB cradle from the dev Mac (`make install`) diff --git a/palm-client/Makefile b/palm-client/Makefile index dcb3cc9..50e2328 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -41,8 +41,16 @@ build/%.o: src/%.c src/*.h src/ui/*.h | build @mkdir -p $(dir $@) $(CC) $(CFLAGS) -c $< -o $@ -build/$(APP): $(OBJS) - $(CC) $(CFLAGS) -o $@ $(OBJS) +# Two code segments (see src/sections.h): multigen turns visormesh.def into a +# stub and a linker script that place section vmcode2 in its own code resource. +build/$(APP)-sections.s build/$(APP)-sections.ld: $(APP).def | build + m68k-palmos-multigen -b build/$(APP)-sections $(APP).def + +build/$(APP)-sections.o: build/$(APP)-sections.s + $(CC) -c $< -o $@ + +build/$(APP): $(OBJS) build/$(APP)-sections.o build/$(APP)-sections.ld + $(CC) $(CFLAGS) -o $@ $(OBJS) build/$(APP)-sections.o build/$(APP)-sections.ld build/res.stamp: resources/$(APP).rcp src/resources.h resources/smallfont.nfnt | build rm -rf build/res && mkdir -p build/res @@ -50,7 +58,7 @@ build/res.stamp: resources/$(APP).rcp src/resources.h resources/smallfont.nfnt | touch $@ build/$(APP).prc: build/$(APP) build/res.stamp - build-prc --backup -o $@ -n "$(NAME)" -c $(CREATOR) build/$(APP) build/res/*.bin + build-prc -o $@ $(APP).def build/$(APP) build/res/*.bin test: | build gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c diff --git a/palm-client/src/protocol.c b/palm-client/src/protocol.c index c5671cf..c0ec6f9 100644 --- a/palm-client/src/protocol.c +++ b/palm-client/src/protocol.c @@ -1,5 +1,11 @@ #include "protocol.h" +/* In the second code segment: see sections.h for the rules. */ +static UInt32 GetLE32(const UInt8 *p) VM_SECT2; +static void CopyBytes(UInt8 *dst, const UInt8 *src, UInt16 n) VM_SECT2; +static void CopyName(Char *dst, UInt16 dstSize, const UInt8 *src, UInt16 n) VM_SECT2; +static void PutLE32(UInt8 *p, UInt32 v) VM_SECT2; + enum { ST_SYNC, ST_LEN_LO, ST_LEN_HI, ST_PAYLOAD }; #define DEVICE_INFO_MIN_LEN 80 /* through the version string */ @@ -351,7 +357,7 @@ UInt16 VmBuildSendAdvert(UInt8 *out, UInt16 outSize, Boolean flood) /* CMD_REBOOT: code + "reboot" (the firmware checks the word, so a stray byte can't reboot it). */ UInt16 VmBuildReboot(UInt8 *out, UInt16 outSize) { - static const UInt8 p[7] = { VM_CMD_REBOOT, 'r', 'e', 'b', 'o', 'o', 't' }; + static UInt8 p[7] = { VM_CMD_REBOOT, 'r', 'e', 'b', 'o', 'o', 't' }; /* not const: sections.h */ return VmEncodeFrame(out, outSize, p, sizeof(p)); } diff --git a/palm-client/src/protocol.h b/palm-client/src/protocol.h index 8e88acd..33ea32e 100644 --- a/palm-client/src/protocol.h +++ b/palm-client/src/protocol.h @@ -14,6 +14,8 @@ #ifndef VM_PROTOCOL_H #define VM_PROTOCOL_H +#include "sections.h" /* this module is in the second code segment */ + #ifdef __palmos__ #include #else @@ -191,48 +193,48 @@ typedef struct { * Returns false on anything but digits with at most one '.' and 'scale' decimals. * VmFormatDecimal(910525, 3) = "910.525", (62500, 3) = "62.5", (921000, 3) = "921". */ -Boolean VmParseDecimal(const Char *s, UInt16 scale, UInt32 *out); -void VmFormatDecimal(Char *out, UInt32 value, UInt16 scale); +Boolean VmParseDecimal(const Char *s, UInt16 scale, UInt32 *out) VM_SECT2; +void VmFormatDecimal(Char *out, UInt32 value, UInt16 scale) VM_SECT2; /* Optional leading '-', then digits. */ -Boolean VmParseInt(const Char *s, Int32 *out); +Boolean VmParseInt(const Char *s, Int32 *out) VM_SECT2; /* Framing */ -UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len); -void VmDecoderReset(VmFrameDecoder *d); -Boolean VmDecoderFeed(VmFrameDecoder *d, UInt8 byte); +UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len) VM_SECT2; +void VmDecoderReset(VmFrameDecoder *d) VM_SECT2; +Boolean VmDecoderFeed(VmFrameDecoder *d, UInt8 byte) VM_SECT2; /* Command builders: each writes a complete '<' frame and returns its length, or 0 if it won't fit. */ -UInt16 VmBuildDeviceQuery(UInt8 *out, UInt16 outSize); -UInt16 VmBuildAppStart(UInt8 *out, UInt16 outSize, const Char *appName); -UInt16 VmBuildGetContacts(UInt8 *out, UInt16 outSize, UInt32 since); -UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize); -UInt16 VmBuildSetAdvertName(UInt8 *out, UInt16 outSize, const Char *name); +UInt16 VmBuildDeviceQuery(UInt8 *out, UInt16 outSize) VM_SECT2; +UInt16 VmBuildAppStart(UInt8 *out, UInt16 outSize, const Char *appName) VM_SECT2; +UInt16 VmBuildGetContacts(UInt8 *out, UInt16 outSize, UInt32 since) VM_SECT2; +UInt16 VmBuildSyncNextMessage(UInt8 *out, UInt16 outSize) VM_SECT2; +UInt16 VmBuildSetAdvertName(UInt8 *out, UInt16 outSize, const Char *name) VM_SECT2; UInt16 VmBuildSetRadioParams(UInt8 *out, UInt16 outSize, UInt32 freqKHz, UInt32 bwHz, UInt8 sf, UInt8 cr, - Boolean includeRepeat, Boolean repeat); -UInt16 VmBuildSetTxPower(UInt8 *out, UInt16 outSize, Int8 dbm); -UInt16 VmBuildSetPathHashMode(UInt8 *out, UInt16 outSize, UInt8 mode); -UInt16 VmBuildGetChannel(UInt8 *out, UInt16 outSize, UInt8 idx); + Boolean includeRepeat, Boolean repeat) VM_SECT2; +UInt16 VmBuildSetTxPower(UInt8 *out, UInt16 outSize, Int8 dbm) VM_SECT2; +UInt16 VmBuildSetPathHashMode(UInt8 *out, UInt16 outSize, UInt8 mode) VM_SECT2; +UInt16 VmBuildGetChannel(UInt8 *out, UInt16 outSize, UInt8 idx) VM_SECT2; /* Direct message: text is UTF-8 bytes, prefix the first 6 bytes of the recipient's key. */ UInt16 VmBuildSendTxt(UInt8 *out, UInt16 outSize, const UInt8 *prefix6, UInt32 timestamp, UInt8 attempt, - const UInt8 *text, UInt16 textLen); + const UInt8 *text, UInt16 textLen) VM_SECT2; UInt16 VmBuildSendChannelTxt(UInt8 *out, UInt16 outSize, UInt8 channelIdx, UInt32 timestamp, - const UInt8 *text, UInt16 textLen); -UInt16 VmBuildGetTime(UInt8 *out, UInt16 outSize); -UInt16 VmBuildSetTime(UInt8 *out, UInt16 outSize, UInt32 unixSeconds); -UInt16 VmBuildSendAdvert(UInt8 *out, UInt16 outSize, Boolean flood); + const UInt8 *text, UInt16 textLen) VM_SECT2; +UInt16 VmBuildGetTime(UInt8 *out, UInt16 outSize) VM_SECT2; +UInt16 VmBuildSetTime(UInt8 *out, UInt16 outSize, UInt32 unixSeconds) VM_SECT2; +UInt16 VmBuildSendAdvert(UInt8 *out, UInt16 outSize, Boolean flood) VM_SECT2; /* CMD_REBOOT: the radio restarts without replying. */ -UInt16 VmBuildReboot(UInt8 *out, UInt16 outSize); +UInt16 VmBuildReboot(UInt8 *out, UInt16 outSize) VM_SECT2; /* Reply parsers: take a decoded payload (code byte first). Return true if it has the expected code and length. */ -Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out); -Boolean VmParseSelfInfo(const UInt8 *p, UInt16 len, VmSelfInfo *out); -Boolean VmParseContactsStart(const UInt8 *p, UInt16 len, UInt32 *count); -Boolean VmParseContact(const UInt8 *p, UInt16 len, VmContact *out); -Boolean VmParseEndOfContacts(const UInt8 *p, UInt16 len, UInt32 *lastMod); -Boolean VmParseMessage(const UInt8 *p, UInt16 len, VmMessage *out); -Boolean VmParseChannelInfo(const UInt8 *p, UInt16 len, VmChannel *out); -Boolean VmParseSent(const UInt8 *p, UInt16 len, VmSent *out); -Boolean VmParseSendConfirmed(const UInt8 *p, UInt16 len, UInt32 *ack, UInt32 *tripMs); -Boolean VmParseCurrTime(const UInt8 *p, UInt16 len, UInt32 *unixSeconds); +Boolean VmParseDeviceInfo(const UInt8 *p, UInt16 len, VmDeviceInfo *out) VM_SECT2; +Boolean VmParseSelfInfo(const UInt8 *p, UInt16 len, VmSelfInfo *out) VM_SECT2; +Boolean VmParseContactsStart(const UInt8 *p, UInt16 len, UInt32 *count) VM_SECT2; +Boolean VmParseContact(const UInt8 *p, UInt16 len, VmContact *out) VM_SECT2; +Boolean VmParseEndOfContacts(const UInt8 *p, UInt16 len, UInt32 *lastMod) VM_SECT2; +Boolean VmParseMessage(const UInt8 *p, UInt16 len, VmMessage *out) VM_SECT2; +Boolean VmParseChannelInfo(const UInt8 *p, UInt16 len, VmChannel *out) VM_SECT2; +Boolean VmParseSent(const UInt8 *p, UInt16 len, VmSent *out) VM_SECT2; +Boolean VmParseSendConfirmed(const UInt8 *p, UInt16 len, UInt32 *ack, UInt32 *tripMs) VM_SECT2; +Boolean VmParseCurrTime(const UInt8 *p, UInt16 len, UInt32 *unixSeconds) VM_SECT2; #endif diff --git a/palm-client/src/sections.h b/palm-client/src/sections.h new file mode 100644 index 0000000..2307344 --- /dev/null +++ b/palm-client/src/sections.h @@ -0,0 +1,22 @@ +/* + * Code segments. The app's code is close to the ~32 KB a single 68k code + * resource can reach, so some modules live in a second one (see visormesh.def). + * + * Rules for a module in VM_SECT2 (prc-tools multiple code sections): + * - Every function in it, static ones too, is declared VM_SECT2, and callers + * see that declaration: calls into another segment go through a jump table. + * - No static const tables and no string literals: those stay in the first + * segment, and code in this one can't reach them PC-relative. Use plain + * (non-const) data, which is A5-relative. + * On the host (tests) the attribute compiles away. + */ +#ifndef VM_SECTIONS_H +#define VM_SECTIONS_H + +#ifdef __palmos__ +#define VM_SECT2 __attribute__ ((section ("vmcode2"))) +#else +#define VM_SECT2 +#endif + +#endif diff --git a/palm-client/src/ui/text.c b/palm-client/src/ui/text.c index 1d09ba6..225b7cd 100644 --- a/palm-client/src/ui/text.c +++ b/palm-client/src/ui/text.c @@ -1,6 +1,9 @@ #include "text.h" #include "fontmetrics.h" +/* In the second code segment: see sections.h for the rules. */ +static Char Cp1252(UInt32 cp) VM_SECT2; + Int16 TxtSmallWidth(Char c) { return gSmallFontWidths[(UInt8)c]; @@ -189,7 +192,7 @@ void TxtUtf8NameToPalm(Char *name, UInt16 size) } /* Windows-1252 0x80-0x9F to Unicode (0 = unused slot). */ -static const UInt16 kCp1252[32] = { +static UInt16 kCp1252[32] = { /* not const: sections.h */ 0x20AC, 0, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0, 0x017D, 0, 0, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, diff --git a/palm-client/src/ui/text.h b/palm-client/src/ui/text.h index 8ec1055..ad8cbcf 100644 --- a/palm-client/src/ui/text.h +++ b/palm-client/src/ui/text.h @@ -5,18 +5,18 @@ #ifndef VM_TEXT_H #define VM_TEXT_H -#include "../protocol.h" +#include "../protocol.h" /* also brings sections.h: this module is in the second code segment */ /* Width of one character in pixels (e.g. from gSmallFontWidths, or FntCharWidth on the device). */ typedef Int16 (*TxtWidthFn)(Char c); /* The small font's widths from the generated table. */ -Int16 TxtSmallWidth(Char c); +Int16 TxtSmallWidth(Char c) VM_SECT2; -Int16 TxtWidth(const Char *s, UInt16 len, TxtWidthFn w); +Int16 TxtWidth(const Char *s, UInt16 len, TxtWidthFn w) VM_SECT2; /* How many characters of s fit in maxW pixels. */ -UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w); +UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w) VM_SECT2; /* * One line of word wrap: returns how many characters to draw on this line @@ -24,35 +24,35 @@ UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w); * than the line) and sets *consumed to that plus any spaces to skip before * the next line. Returns 0 with *consumed 0 only for empty input. */ -UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed); +UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed) VM_SECT2; /* "now", "5m", "23h", "3d", "2y" (MeshTerm's aligned-column ages). */ -void TxtFormatAge(Char *out, UInt32 seconds); +void TxtFormatAge(Char *out, UInt32 seconds) VM_SECT2; /* SNR in quarter dB as a whole number: "+6dB", "-3dB", "0dB". */ -void TxtFormatSnr(Char *out, Int8 quarterDb); +void TxtFormatSnr(Char *out, Int8 quarterDb) VM_SECT2; /* * MeshCore channel messages carry the sender in the text as "Name: message". * Returns the name's length (0 if there's no such prefix); the message starts * at name length + 2. */ -UInt16 TxtSplitSender(const Char *text, UInt16 len); +UInt16 TxtSplitSender(const Char *text, UInt16 len) VM_SECT2; /* * UTF-8 to the Visor's Latin-1 / Windows-1252 character set. Characters it * can't show become '?'. Writes at most outSize - 1 bytes plus a terminator * and returns the length. */ -UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize); +UInt16 TxtUtf8ToPalm(const UInt8 *in, UInt16 inLen, Char *out, UInt16 outSize) VM_SECT2; /* Converts a NUL-terminated UTF-8 name in place to the Visor's charset (size <= 48). */ -void TxtUtf8NameToPalm(Char *name, UInt16 size); +void TxtUtf8NameToPalm(Char *name, UInt16 size) VM_SECT2; /* * The Visor's text (Windows-1252) to UTF-8 for sending. Returns the byte count, * or 0 if it won't fit in outSize (so a message is never cut mid-character). */ -UInt16 TxtPalmToUtf8(const Char *in, UInt16 inLen, UInt8 *out, UInt16 outSize); +UInt16 TxtPalmToUtf8(const Char *in, UInt16 inLen, UInt8 *out, UInt16 outSize) VM_SECT2; #endif diff --git a/palm-client/visormesh.def b/palm-client/visormesh.def new file mode 100644 index 0000000..21ce7dd --- /dev/null +++ b/palm-client/visormesh.def @@ -0,0 +1,2 @@ +application { "VisorMesh" VMsh backup } +multiple code { "vmcode2" }