From 4316f928f8944dd278ccb05a35dd5ba7ca91ae7a Mon Sep 17 00:00:00 2001 From: Mirko Scholz Date: Tue, 8 Sep 2020 23:46:41 +0200 Subject: [PATCH] added facility to debug the stack pointer on P3_4 (LCD LED cathode) roughly 139 bytes functions were chosen by manually traversing pow_decn to the bottom --- Makefile | 7 ++-- src/calc.c | 1 + src/decn/CMakeLists.txt | 2 +- src/decn/decn.c | 4 ++ src/lcd.c | 6 +++ src/main.c | 7 +++- src/stack_debug.c | 92 +++++++++++++++++++++++++++++++++++++++++ src/stack_debug.h | 54 ++++++++++++++++++++++++ src/utils.c | 5 --- src/utils.h | 5 --- 10 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 src/stack_debug.c create mode 100644 src/stack_debug.h diff --git a/Makefile b/Makefile index ef9bb46..ff4d184 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,9 @@ SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 256 --idata-loc 0x80 #SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 256 --stack-auto --model-large FLASHFILE ?= main.hex LARGE_LDFLAGS += -L/usr/share/sdcc/lib/large/ +# CFLAGS += -DSTACK_DEBUG # write the stack pointer to P3_4 -SRC = src/lcd.c src/key.c src/utils.c src/decn/decn.c src/calc.c +SRC = src/lcd.c src/key.c src/utils.c src/decn/decn.c src/calc.c src/stack_debug.c OBJ=$(patsubst src%.c,build%.rel, $(SRC)) @@ -13,7 +14,7 @@ all: main build/%.rel: src/%.c src/%.h mkdir -p $(dir $@) - $(SDCC) $(SDCCOPTS) -o $@ -c $< + $(SDCC) $(SDCCOPTS) $(CFLAGS) -o $@ -c $< main: $(OBJ) $(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $^ @@ -21,7 +22,7 @@ main: $(OBJ) @ tail -n 5 build/main.mem | head -n 2 @ tail -n 1 build/main.mem cp build/$@.ihx $@.hex - + eeprom: sed -ne '/:..1/ { s/1/0/2; p }' main.hex > eeprom.hex diff --git a/src/calc.c b/src/calc.c index a89e446..9270262 100644 --- a/src/calc.c +++ b/src/calc.c @@ -21,6 +21,7 @@ #include "utils.h" #include "calc.h" +#include "stack_debug.h" __xdata dec80 StoredDecn; __xdata dec80 LastX; diff --git a/src/decn/CMakeLists.txt b/src/decn/CMakeLists.txt index 246c619..7abc9a6 100644 --- a/src/decn/CMakeLists.txt +++ b/src/decn/CMakeLists.txt @@ -12,7 +12,7 @@ target_compile_options(coverage_config INTERFACE -O0 -g --coverage) target_link_libraries(coverage_config INTERFACE --coverage) # decn library -add_library(decn decn.c) +add_library(decn decn.c ../utils.c) # decn library with coverage add_library(decn_cover decn.c) diff --git a/src/decn/decn.c b/src/decn/decn.c index 909edb2..b669d05 100644 --- a/src/decn/decn.c +++ b/src/decn/decn.c @@ -19,6 +19,7 @@ */ #include "../utils.h" +#include "../stack_debug.h" #include "decn.h" @@ -86,6 +87,8 @@ const dec80 DECN_LN_10 = { void copy_decn(dec80* const dest, const dec80* const src){ uint8_t i; + + stack_debug(0x01); dest->exponent = src->exponent; //copy nibbles @@ -163,6 +166,7 @@ static void remove_leading_zeros(dec80* x){ uint8_t is_negative = (x->exponent < 0); exp_t exponent = get_exponent(x); + stack_debug(0x02); //find first non-zero digit100 for (digit100 = 0; digit100 < DEC80_NUM_LSU; digit100++){ if (x->lsu[digit100] != 0){ diff --git a/src/lcd.c b/src/lcd.c index e2253c9..b5eac5c 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -155,9 +155,15 @@ void LCD_Open(void) { //P2 entire port P2M1 = 0; P2M0 = 0xff; +#ifdef STACK_DEBUG + // P3_4 is special + P3M1 &= ~(0xe0); + P3M0 |= (0xe0); +#else //P3 pins 7:4 P3M1 &= ~(0xf0); P3M0 |= (0xf0); +#endif _delay_ms(30); // to allow LCD powerup outCsrBlindNibble(0x03); // (DL=1 8-bit mode) diff --git a/src/main.c b/src/main.c index 961fc1c..613c600 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #else #include "stc15.h" #endif +#include "stack_debug.h" #define FOSC 11583000 @@ -225,7 +226,9 @@ int main() LCD_Open(); KeyInit(); Timer0Init(); //for reading keyboard - BACKLIGHT_ON(); //turn on led backlight + backlight_on(); //turn on led backlight + stack_debug_init(); + stack_debug(0xfe); ExpBuf[0] = 0; ExpBuf[1] = 0; @@ -529,7 +532,7 @@ int main() LcdAvailable.release(); #endif //turn backlight back on - BACKLIGHT_ON(); + backlight_on(); } //while (1) } /* ------------------------------------------------------------------------- */ diff --git a/src/stack_debug.c b/src/stack_debug.c new file mode 100644 index 0000000..859d990 --- /dev/null +++ b/src/stack_debug.c @@ -0,0 +1,92 @@ +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include +#include "stack_debug.h" + +#if defined(STACK_DEBUG) +extern uint16_t _SP; + +inline static void HIGH() { + P3_4 = 1; +} + +inline static void LOW() { + P3_4 = 1; +} + +void stack_debug_init() { + P3M1 &= ~(0x10); + P3M0 |= 0x10; + LOW(); + stack_debug_write(0x55); + stack_debug_write(0xAA); + stack_debug_write(0x55); + stack_debug_write(0xAA); + stack_debug_write(0x80); + stack_debug_write(0x10); + stack_debug_write(0x08); + stack_debug_write(0x01); +} + +/* + for frequency of 12.000 MHz (12.005 adjusted) this gives a rate of 1'190'000 Baud + negative polarity, MSB + */ + +void stack_debug_write(uint8_t value) __naked { + value; +__asm + .macro HIGH + setb _P3_4 + .endm + + .macro LOW + clr _P3_4 + .endm + + mov dph,r0 + mov a,dpl + mov r0,#8 + ; START + HIGH ; 1 + nop ; 1 + nop ; 1 + nop ; 1 +stack_debug_1$: + rlc a ; 1 + jc stack_debug_2$ ; 3 + HIGH ; 1 + djnz r0,stack_debug_1$ ; 4 + sjmp stack_debug_3$ ; 3 +stack_debug_2$: + LOW ; 1 + djnz r0,stack_debug_1$ ; 4 + sjmp stack_debug_3$ ; 3 +stack_debug_3$: + nop ; 1 + ; STOP + LOW ; 1 + nop ; 1 + nop ; 1 + + mov r0,dph ; 3 + ret ; 4 +__endasm; +} + +void stack_debug(uint8_t marker) { + stack_debug_write(marker); + stack_debug_write(SP); +} +#endif // !defined(DESKTOP) diff --git a/src/stack_debug.h b/src/stack_debug.h new file mode 100644 index 0000000..a22567e --- /dev/null +++ b/src/stack_debug.h @@ -0,0 +1,54 @@ +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +#ifndef SRC_STACK_DEBUG_H_ +#define SRC_STACK_DEBUG_H_ + +#include + +#if !defined(DESKTOP) +#include "stc15.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// P3_4 is connected to the cathode and the only pin that is not toggled normally +// unfortunately, the hardware uart cannot be mapped to TX = P3_4 + +#if defined(STACK_DEBUG) +void stack_debug_init(void); +void stack_debug(uint8_t marker); +void stack_debug_write(uint8_t value) __naked; +#else +#define stack_debug_init() +#define stack_debug(marker) +#endif + +#if defined(DESKTOP) || defined(STACK_DEBUG) +#define backlight_on() +#define backlight_off() +#else +inline void backlight_on(void) { + P3_4 = 0; +} +inline void backlight_off(void) { + P3_4 = 1; +} +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/utils.c b/src/utils.c index 38680ed..f92f994 100644 --- a/src/utils.c +++ b/src/utils.c @@ -29,7 +29,6 @@ void _delay_us(uint8_t us) (void) us; } #endif -void backlight_off(void){ } #else //!DESKTOP void _delay_ms(uint8_t ms) { @@ -69,10 +68,6 @@ void _delay_us(uint8_t us) } #endif -void backlight_off(void){ - P3_4 = 1; -} - #endif //ifdef desktop #ifdef DESKTOP diff --git a/src/utils.h b/src/utils.h index 192dc5e..c4373bf 100644 --- a/src/utils.h +++ b/src/utils.h @@ -35,9 +35,6 @@ void _delay_us(uint8_t us); #define _delay_us(x) _delay_ms(1) #endif -void backlight_off(void); - - #ifdef __linux__ #define DESKTOP @@ -58,11 +55,9 @@ char* u32str(uint32_t x, char* buf, uint8_t base); #define __at uint8_t* #define SDCC_ISR(isr, reg) #define __using(x) -#define BACKLIGHT_ON() #define TURN_OFF() #else #define SDCC_ISR(isr, reg) __interrupt (isr) __using (reg) -#define BACKLIGHT_ON() P3_4 = 0 #define TURN_OFF() P3_2 = 0 #endif