From 18f1fb5f1a08c3995a21df1b9c18e37a1fea96e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Wed, 10 Sep 2025 16:07:59 +0200 Subject: [PATCH 1/6] http: don't inline char_to_html(), save 1k of flash --- httpd/page_impl.c | 2 +- rtlplayground.mem | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 263333d..7ed6c94 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -34,7 +34,7 @@ inline void byte_to_html(uint8_t a) } -inline void char_to_html(char c) +void char_to_html(char c) { outbuf[slen++] = c; } diff --git a/rtlplayground.mem b/rtlplayground.mem index 0259cd7..03fcd49 100644 --- a/rtlplayground.mem +++ b/rtlplayground.mem @@ -25,5 +25,5 @@ Other memory: Name Start End Size Max ---------------- -------- -------- -------- -------- PAGED EXT. RAM 0 256 - EXTERNAL RAM 0x0001 0x1ae8 6888 16777216 - ROM/EPROM/FLASH 0x0000 0x1c69f 46879 16777216 + EXTERNAL RAM 0x0001 0x1b28 6952 16777216 + ROM/EPROM/FLASH 0x0000 0x1bf4f 45844 16777216 From 24f588effd56257b28fc38efdc0dd8905d16f488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Wed, 10 Sep 2025 16:07:59 +0200 Subject: [PATCH 2/6] httpd: Also don't byte_to_html() and itoa_html() to save 1.5kbytes. Also fixed compiler "warning page_impl.c:47: warning 126: unreachable code". But I think it was a false-positive. --- httpd/page_impl.c | 36 ++++++++++++++++++++++++++++++++---- rtlplayground.mem | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 7ed6c94..1cff493 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -27,10 +27,26 @@ extern __xdata uint8_t isRTL8373; extern __xdata uint8_t sfp_pins_last; extern __xdata uint8_t vlan_names[VLAN_NAMES_SIZE]; -inline void byte_to_html(uint8_t a) + +/* Convert only the lower nibble to ascii HEX char. + For convenience the upper nibble is masked out. +*/ +inline char itohex(uint8_t val) { + // Ignore upper nibble for convenience. + val &= 0x0f; + val -= 10; + + // 10 or above + if ((int8_t)val >= 0) + val += ('a' - '0' - 10); + + return val + ('0' + 10); +} + +// Convert uint8_t to ascii HEX char push on html-buffer. +void charhex_to_html(char c) { - outbuf[slen++] = hex[(a >> 4) & 0xf]; - outbuf[slen++] = hex[a & 0xf]; + outbuf[slen++] = itohex(c); } @@ -40,7 +56,19 @@ void char_to_html(char c) } -inline void itoa_html(uint8_t v) +// Convert uint8_t to ascii HEX char. +void byte_to_html(uint8_t val) +{ + uint8_t cnt = 2; + do { + val = (val >> 4) | (val << 4); + charhex_to_html(val); + cnt -= 1; + } while(cnt); +} + + +void itoa_html(uint8_t v) { uint8_t t = (v / 100) % 10; if (t) diff --git a/rtlplayground.mem b/rtlplayground.mem index 03fcd49..c653536 100644 --- a/rtlplayground.mem +++ b/rtlplayground.mem @@ -26,4 +26,4 @@ Other memory: ---------------- -------- -------- -------- -------- PAGED EXT. RAM 0 256 EXTERNAL RAM 0x0001 0x1b28 6952 16777216 - ROM/EPROM/FLASH 0x0000 0x1bf4f 45844 16777216 + ROM/EPROM/FLASH 0x0000 0x1b967 44332 16777216 From 94ae1472c368a0999edc85dcfe4ac6d538d8d47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Wed, 10 Sep 2025 16:07:59 +0200 Subject: [PATCH 3/6] httpd: optimize sfr_data_to_html() This also fixes a compiler warning. page_impl.c: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG --- httpd/page_impl.c | 47 +++++++++++++++++++---------------------------- rtlplayground.mem | 2 +- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 1cff493..0888bf2 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -94,39 +94,30 @@ uint16_t port_status(void) } +/* Converts sfr_data[] into raw hex string. + Suppress leading zeros. +*/ void sfr_data_to_html(void) { - __bit print_zeros = 0; + uint8_t print_zeros = 0; + uint8_t val = 0; - if (print_zeros || sfr_data[0] & 0xf0) { - char_to_html(hex[sfr_data[0] >> 4]); - print_zeros = 1; + for (uint8_t nibble = 0; nibble < 8; nibble++) { + if (!(nibble & 1)) + val = sfr_data[nibble>>1]; + // force the swap instruction, itohex() ignores the upper nibble. + val = (val << 4) | (val >> 4); + // when print_zeros is not zero, we know that a non-zero number has printed. + // That have to print all the next numbers. + print_zeros |= val; + // only care about lower nibble, that is what is printed. + print_zeros &= 0x0f; + if (print_zeros) + charhex_to_html(val); } - if (print_zeros || sfr_data[0] & 0xf) { - char_to_html(hex[sfr_data[0] & 0xf]); - print_zeros = 1; + if (print_zeros == 0) { + char_to_html('0'); } - if (print_zeros || sfr_data[1] & 0xf0) { - char_to_html(hex[sfr_data[1] >> 4]); - print_zeros = 1; - } - if (print_zeros || sfr_data[1] & 0xf) { - char_to_html(hex[sfr_data[1] & 0xf]); - print_zeros = 1; - } - if (print_zeros || sfr_data[2] & 0xf0) { - char_to_html(hex[sfr_data[2] >> 4]); - print_zeros = 1; - } - if (print_zeros || sfr_data[2] & 0xf) { - char_to_html(hex[sfr_data[2] & 0xf]); - print_zeros = 1; - } - if (print_zeros || sfr_data[3] & 0xf0) { - char_to_html(hex[sfr_data[3] >> 4]); - print_zeros = 1; - } - char_to_html(hex[sfr_data[3] & 0xf]); } diff --git a/rtlplayground.mem b/rtlplayground.mem index c653536..0fb7e78 100644 --- a/rtlplayground.mem +++ b/rtlplayground.mem @@ -26,4 +26,4 @@ Other memory: ---------------- -------- -------- -------- -------- PAGED EXT. RAM 0 256 EXTERNAL RAM 0x0001 0x1b28 6952 16777216 - ROM/EPROM/FLASH 0x0000 0x1b967 44332 16777216 + ROM/EPROM/FLASH 0x0000 0x1b84f 44052 16777216 From 8ff124af3092a83ee184b45f4ae3f1e76f93b2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Wed, 10 Sep 2025 16:07:59 +0200 Subject: [PATCH 4/6] httpd: fix itoa_html() does not print the middle zero. when the input is 100. the first zero is not printed because t = 0 and skip printing. So the output is "10" instead of "100". --- httpd/page_impl.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 0888bf2..79f62f8 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -67,14 +67,20 @@ void byte_to_html(uint8_t val) } while(cnt); } - +/* Converts a uint8_t to raw string. + Suppress leading zeros. +*/ void itoa_html(uint8_t v) { - uint8_t t = (v / 100) % 10; - if (t) + uint8_t t = (v / 100); + // when print_zeros is not zero, we know that a non-zero number has printed. + // That have to print all the next numbers. + uint8_t print_zeros = t; + if (print_zeros) char_to_html('0' + t); t = (v / 10) % 10; - if (t) + print_zeros |= t; + if (print_zeros) char_to_html('0' + t); char_to_html('0' + (v % 10)); } From f148bdad201c427f32263f01261a6bcfc85f00fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Wed, 10 Sep 2025 16:07:59 +0200 Subject: [PATCH 5/6] port: fix REG_SET() compiler warning. Compiler rtl837x_port.c:466: warning 116: right shifting more than size of object changed to zero provided value is a uint16. So shifting it more then 16 cause this error. Solution: cast the value to uint32_t. --- rtl837x_regs.h | 12 ++++++------ rtlplayground.mem | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rtl837x_regs.h b/rtl837x_regs.h index 027d926..2ab37db 100644 --- a/rtl837x_regs.h +++ b/rtl837x_regs.h @@ -158,9 +158,9 @@ #ifdef REGDBG -#define REG_SET(r, v) SFR_DATA_24 = ((v) >> 24) & 0xff; \ - SFR_DATA_16 = ((v) >> 16) & 0xff; \ - SFR_DATA_8 = ((v) >> 8 & 0xff); \ +#define REG_SET(r, v) SFR_DATA_24 = (((uint32_t)v) >> 24) & 0xff; \ + SFR_DATA_16 = (((uint32_t)v) >> 16) & 0xff; \ + SFR_DATA_8 = (((uint16_t)v) >> 8 & 0xff); \ SFR_DATA_0 = (v) & 0xff; \ reg_write(r); \ write_char('R'); print_byte(r >> 8); print_byte(r); write_char('-'); \ @@ -173,9 +173,9 @@ reg_write(r); \ write_char('R'); print_byte(r>>8); print_byte(r); write_char('-'); print_byte(v24); print_byte(v16); print_byte(v8); print_byte(v0); write_char(' '); #else -#define REG_SET(r, v) SFR_DATA_24 = ((v) >> 24) & 0xff; \ - SFR_DATA_16 = ((v) >> 16) & 0xff; \ - SFR_DATA_8 = ((v) >> 8 & 0xff); \ +#define REG_SET(r, v) SFR_DATA_24 = (((uint32_t)v) >> 24) & 0xff; \ + SFR_DATA_16 = (((uint32_t)v) >> 16) & 0xff; \ + SFR_DATA_8 = (((uint16_t)v) >> 8 & 0xff); \ SFR_DATA_0 = (v) & 0xff; \ reg_write(r); diff --git a/rtlplayground.mem b/rtlplayground.mem index 0fb7e78..d7891a6 100644 --- a/rtlplayground.mem +++ b/rtlplayground.mem @@ -26,4 +26,4 @@ Other memory: ---------------- -------- -------- -------- -------- PAGED EXT. RAM 0 256 EXTERNAL RAM 0x0001 0x1b28 6952 16777216 - ROM/EPROM/FLASH 0x0000 0x1b84f 44052 16777216 + ROM/EPROM/FLASH 0x0000 0x1b85c 44065 16777216 From 2eb1416adaaca56dfc0c9e55abfb053fd3390083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Thu, 11 Sep 2025 07:47:33 +0200 Subject: [PATCH 6/6] httpd: fix print ip-address, added missing shift. --- httpd/page_impl.c | 2 +- rtlplayground.mem | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/httpd/page_impl.c b/httpd/page_impl.c index 79f62f8..89ae166 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -141,7 +141,7 @@ uint16_t html_index(void) itoa_html(uip_hostaddr[0]); char_to_html('.'); itoa_html(uip_hostaddr[0] >> 8); char_to_html('.'); itoa_html(uip_hostaddr[1]); char_to_html('.'); - itoa_html(uip_hostaddr[1]); + itoa_html(uip_hostaddr[1] >> 8); slen += strtox(outbuf + slen, "Gateway"); itoa_html(uip_draddr[0]); char_to_html('.'); itoa_html(uip_draddr[0] >> 8); char_to_html('.'); diff --git a/rtlplayground.mem b/rtlplayground.mem index d7891a6..acbb192 100644 --- a/rtlplayground.mem +++ b/rtlplayground.mem @@ -26,4 +26,4 @@ Other memory: ---------------- -------- -------- -------- -------- PAGED EXT. RAM 0 256 EXTERNAL RAM 0x0001 0x1b28 6952 16777216 - ROM/EPROM/FLASH 0x0000 0x1b85c 44065 16777216 + ROM/EPROM/FLASH 0x0000 0x1b85e 44067 16777216