attempt to reduce space usage

- remove unecessary code
- ifdef "extra" code
- pass data through globals instead of functions accessing static data
- functions non-reentrant, locate tmps in xdata
This commit is contained in:
Jeff Wang 2019-03-31 19:29:37 -04:00
parent ecb0492534
commit 43edb291cf
8 changed files with 132 additions and 99 deletions

View File

@ -1,7 +1,9 @@
SDCC ?= sdcc
STCCODESIZE ?= 13000
SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 0 --stack-auto
STCCODESIZE ?= 13312
SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 256 --stack-auto --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/
SRC = src/lcd.c src/key.c src/utils.c src/decn/decn.c src/calc.c
@ -15,6 +17,7 @@ build/%.rel: src/%.c src/%.h
main: $(OBJ)
$(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $^
# $(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(CFLAGS) $(LARGE_LDFLAGS) $^
@ tail -n 5 build/main.mem | head -n 2
@ tail -n 1 build/main.mem
cp build/$@.ihx $@.hex

View File

@ -7,13 +7,16 @@
#include "../utils.h"
#define EXTRA_CHECKS
//#define DEBUG
//#define DEBUG_COMPARE_MAGN
//#define DEBUG_ADD
//#define DEBUG_MULT
//#define DEBUG_MULT_ALL //even more verbose
//#define DEBUG_DIV
#ifndef DESKTOP
//#undef EXTRA_CHECKS
#undef DEBUG
#undef DEBUG_COMPARE_MAGN
#undef DEBUG_ADD
@ -34,9 +37,9 @@
#include "decn.h"
#ifdef DESKTOP
uint8_t num_digits_display = DEC80_NUM_LSU*2;
static const uint8_t num_digits_display = DEC80_NUM_LSU*2;
#else
uint8_t num_digits_display = 16;
static const uint8_t num_digits_display = 16;
#endif
@ -168,7 +171,9 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
if (!SEEN_POINT(curr_sign)){
//begin tracking number of digits to right of decimal point
curr_sign |= 1; //seen point
} else {
}
#ifdef EXTRA_CHECKS
else {
//multiple '.'s in string
#ifdef DEBUG
printf(" ERROR: multiple '.'s in string\n");
@ -176,6 +181,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest);
return;
}
#endif
} else if (signif_str[i] >= '1' && signif_str[i] <= '9'){
if (nibble_i < DEC80_NUM_LSU*2){
if (nibble_i & 1) { //odd
@ -245,6 +251,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
assert(DEC80_NUM_LSU*2 > num_lr_points);
new_exponent = exponent + (num_lr_points - 1); //1 digit left of implicit point
//check for overflow
#ifdef EXTRA_CHECKS
if (new_exponent < exponent || exponent > DEC80_MAX_EXP){
#ifdef DEBUG
printf(" overflow (new_exp, exp)=(%d,%d)\n",
@ -253,12 +260,14 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest);
return;
}
#endif
} else if (num_lr_points < 0) { //right count exists
// (-num_past_point represents #0s right of decimal)
// (this ends up being a subtraction)
new_exponent = exponent + num_lr_points;
new_exponent -= 1; //decimal point after 1st non-zero number
//check for underflow
#ifdef EXTRA_CHECKS
if (new_exponent > exponent || exponent < DEC80_MIN_EXP){
#ifdef DEBUG
printf(" underflow (new_exp, exp)=(%d,%d)\n",
@ -267,6 +276,7 @@ void build_dec80(dec80* dest, const char* signif_str, int16_t exponent){
set_dec80_NaN(dest);
return;
}
#endif
} else {
//no change
new_exponent = exponent;
@ -319,6 +329,7 @@ static uint8_t decn_is_zero(const dec80* x){
return 1;
}
#ifdef EXTRA_CHECKS
void set_dec80_NaN(dec80* dest){
uint8_t i;
@ -329,6 +340,7 @@ void set_dec80_NaN(dec80* dest){
dest->lsu[i] = 0;
}
}
#endif
void negate_decn(dec80* x){
static const int16_t xor_val = -(0x7fff) - 1;
@ -339,7 +351,7 @@ int8_t compare_magn(const dec80* a, const dec80* b){ //a<b: -1, a==b: 0, a>b: 1
uint8_t a_i, b_i;
int16_t a_exp=0, b_exp=0;
int8_t a_signif_b = 0; //a<b: -1, a==b: 0, a>b: 1
dec80 a_tmp, b_tmp;
static __xdata dec80 a_tmp, b_tmp;
//copy
copy_decn(&a_tmp, a);
copy_decn(&b_tmp, b);
@ -448,7 +460,7 @@ static void _incr_exp(dec80* acc, int16_t exponent){
static void sub_mag(dec80* acc, const dec80* x){
int8_t i;
uint8_t carry = 0;
dec80 tmp;
static __xdata dec80 tmp;
copy_decn(&tmp, x);
//normalize
remove_leading_zeros(acc);
@ -478,7 +490,7 @@ static void sub_mag(dec80* acc, const dec80* x){
}
void add_decn(dec80* acc, const dec80* x){
dec80 tmp;
static __xdata dec80 tmp;
int8_t rel;
uint8_t carry = 0;
int8_t i;
@ -599,8 +611,8 @@ void add_decn(dec80* acc, const dec80* x){
}
void mult_decn(dec80* acc, const dec80* x){
dec80 tmp; //copy of x
dec80 acc_tmp; //holds sum
static __xdata dec80 tmp; //copy of x
static __xdata dec80 acc_tmp; //holds sum
int8_t i, j;
uint8_t carry = 0;
uint8_t is_neg;
@ -631,7 +643,7 @@ void mult_decn(dec80* acc, const dec80* x){
carry = digit100 / 100;
assert(carry < 100);
}
#ifdef DEBUG_MULT
#ifdef DEBUG_MULT_ALL
printf("\n%d:", i);
printf("\n acc:");
for (j = 0; j < DEC80_NUM_LSU; j++){
@ -644,6 +656,8 @@ void mult_decn(dec80* acc, const dec80* x){
else
printf(" ");
}
#endif
#ifdef DEBUG_MULT
printf("\n acc_tmp:");
for (j = 0; j < DEC80_NUM_LSU; j++){
printf(" %3d", acc_tmp.lsu[j]);
@ -681,14 +695,16 @@ void mult_decn(dec80* acc, const dec80* x){
}
void div_decn(dec80* acc, const dec80* x){
dec80 tmp; //copy of x, holds current 1/x estimate
dec80 acc_copy; //holds copy of original acc
static __xdata dec80 tmp; //copy of x, holds current 1/x estimate
static __xdata dec80 acc_copy; //holds copy of original acc
uint8_t i;
//check divide by zero
#ifdef EXTRA_CHECKS
if (decn_is_zero(x)){
set_dec80_NaN(acc);
return;
}
#endif
//store copy of acc for final multiply by 1/x
copy_decn(&acc_copy, acc);
//get initial estimate for 1/x, by negating exponent, and setting signif. to 1
@ -702,24 +718,24 @@ void div_decn(dec80* acc, const dec80* x){
for (i = 0; i < 20; i++){ //just fix number of iterations for now
#ifdef DEBUG_DIV
extern char Buf[80];
dec80_to_str(buf, &tmp);
printf("%2d: %s\n", i, buf);
dec80_to_str(Buf, &tmp);
printf("%2d: %s\n", i, Buf);
#endif
mult_decn(acc, x);
#ifdef DEBUG_DIV
dec80_to_str(buf, acc);
printf(" %20s: %s\n", "recip*x", buf);
dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "recip*x", Buf);
#endif
negate_decn(acc);
add_decn(acc, &DECN_1);
#ifdef DEBUG_DIV
dec80_to_str(buf, acc);
printf(" %20s: %s\n", "(1-recip*x)", buf);
dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "(1-recip*x)", Buf);
#endif
mult_decn(acc, &tmp);
#ifdef DEBUG_DIV
dec80_to_str(buf, acc);
printf(" %20s: %s\n", "recip * (1-recip*x)", buf);
dec80_to_str(Buf, acc);
printf(" %20s: %s\n", "recip * (1-recip*x)", Buf);
#endif
add_decn(acc, &tmp);
//new_est(acc) = recip + (1 - recip*x)*recip, where tmp is current recip estimate
@ -737,7 +753,7 @@ void dec80_to_str(char* buf, const dec80* x){
int16_t exponent = 0;
uint8_t trailing_zeros = 0;
uint8_t use_sci = 0;
dec80 tmp;
static __xdata dec80 tmp;
//copy and normalize
copy_decn(&tmp, x);

View File

@ -7,16 +7,16 @@
#include <stdint.h>
#include "utils.h"
#include "key.h"
#ifndef DESKTOP
#include "stc15.h"
#endif
#define TOTAL_ROWS 5
#define M_COLS 4
static uint8_t keys[TOTAL_ROWS]; //only bottom nibbles get set
uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
static uint32_t new_keys_pressed; //bottom 20 bits get set
static uint8_t last_state[TOTAL_ROWS]; //all used, 2 bits per key
static int8_t last_count[TOTAL_ROWS][M_COLS]; //all used
@ -81,7 +81,7 @@ static void raw_scan(void){
uint8_t i, j;
//scan top row
keys[0] = 0; //initially
Keys[0] = 0; //initially
//temporarily set P3[1:0] push pull output M[1:0]=b01
P3M0 |= 0x3;
P3 &= ~(0x3);
@ -89,19 +89,19 @@ static void raw_scan(void){
//back to quasi-bidirectional w/ pullup M[1:0]=b00
P3M0 &= ~(0x3);
//read inputs
keys[0] += (P3_0 ? 0 : (1<<0)); //on
keys[0] += (P3_1 ? 0 : (1<<1));
keys[0] += (P5_4 ? 0 : (1<<2));
keys[0] += (P5_5 ? 0 : (1<<3));
Keys[0] += (P3_0 ? 0 : (1<<0)); //on
Keys[0] += (P3_1 ? 0 : (1<<1));
Keys[0] += (P5_4 ? 0 : (1<<2));
Keys[0] += (P5_5 ? 0 : (1<<3));
//scan matrix
for (i = 0; i < M_ROWS; i++){
keys[i+1] = 0; //initially
Keys[i+1] = 0; //initially
for (j = 0; j < M_COLS; j++){
uint8_t read;
P1 = (~(1 << (j+4))); //pulldown column (all others pull up)
read = (P1 & (1<<i)); //read button
read = (read == 0 ? 1 : 0); //pressed if grounded by column driver
keys[i+1] += (read << j);
Keys[i+1] += (read << j);
P1 = 0xff; //reset all as pullups
}
}
@ -123,7 +123,7 @@ static void debounce(void){
new_count = curr_count;
new_state = curr_state;
//update count
if (keys[i] & (1 << j)){
if (Keys[i] & (1 << j)){
if (curr_count < COUNT_LIM_HIGH){
new_count = curr_count + 1;
}
@ -198,9 +198,11 @@ void KeyScan(void){
debounce();
}
#ifdef DEBUG_KEYS
const uint8_t* DebugGetKeys(void){
return keys;
}
#endif
const uint32_t GetNewKeys(void){
return new_keys_pressed;

View File

@ -7,10 +7,20 @@
#ifndef SRC_KEY_H_
#define SRC_KEY_H_
//#define DEBUG_KEYS
void KeyInit(void);
void KeyScan(void);
const uint8_t* DebugGetKeys(void);
const uint32_t GetNewKeys(void);
#ifdef DEBUG_KEYS
const uint8_t* DebugGetKeys(void);
#endif
//definition included for determining if multiple keys are pressed
//prefer using GetNewKeys();
#define TOTAL_ROWS 5
extern uint8_t Keys[TOTAL_ROWS]; //only bottom nibbles get set
#endif /* SRC_KEY_H_ */

View File

@ -29,9 +29,6 @@
static int row, col;
static int OpenFlag = 0;
static int ErrorTimeout = 0;
static int Error = 0;
#define CLEAR_BIT(port, bit) (port &= ~(_BV(bit)))
#define CLEAR_BITS(port, bits) (port &= ~(bits))
@ -112,7 +109,6 @@ static char readBusy() {
return busy;
}
//sets ErrorTimeout flag if busy signal not cleared in time
static void wait_busy() {
unsigned int i;
for (i = 0; i < 100; i++){
@ -122,7 +118,6 @@ static void wait_busy() {
_delay_ms(1);
}
ErrorTimeout = 1;
return;
}
@ -148,8 +143,6 @@ static void LCD_OutChar(unsigned char c) {
}
void LCD_Open(void) {
static int open_error = 0;
if (!OpenFlag) {
//set ports to push-pull output M[1:0] = b01
//P2 entire port
P2M1 = 0;
@ -181,16 +174,8 @@ void LCD_Open(void) {
//set 4-bit mode, 2 line, 5x7 display:
outCsrBlind(0x20 + LCD2LINE + LCD7DOT);
//clear display
LCD_Clear();
_delay_ms(50);
if (!readBusy()) {
OpenFlag = 1;
} else {
open_error = 1;
}
} else { //display already open
open_error = 1;
}
// LCD_Clear();
// _delay_ms(50);
}
//row and columns indexed from 0
@ -199,8 +184,6 @@ void LCD_GoTo(unsigned int row_to, unsigned int col_to) {
outCsr(0x80 + 0x40 * row_to + col_to); //set ddram address to position
row = row_to;
col = col_to;
} else {
Error = 1;
}
}
@ -267,19 +250,8 @@ void LCD_OutNibble(uint8_t x){
}
void LCD_Clear() {
if (OpenFlag) {
outCsr(0x01);
} else {
outCsrBlind(0x01);
}
row = 0;
col = 0;
}
unsigned char LCD_Timeout_Error(void) {
if (ErrorTimeout != 0) {
return 1;
}
return 0;
}

View File

@ -25,14 +25,17 @@ static const char KEY_MAP[20] = {
uint32_t NewKeyBuf[4];
volatile uint8_t new_key_write_i;
volatile uint8_t new_key_read_i;
volatile uint8_t new_key_empty;
volatile uint8_t NewKeyEmpty;
#define INCR_NEW_KEY_I(i) i = (i + 1) & 3
volatile uint8_t SecCount;
void timer0_isr() __interrupt 1 __using 1
//#define TRACK_TIME
{
#ifdef TRACK_TIME
static uint8_t count = 0;
static uint8_t min_count = 0, hour_count = 0;
#endif
uint32_t new_keys;
@ -40,16 +43,17 @@ void timer0_isr() __interrupt 1 __using 1
KeyScan();
new_keys = GetNewKeys();
if (new_keys != 0){
if (!new_key_empty && (new_key_write_i == new_key_read_i)){
if (!NewKeyEmpty && (new_key_write_i == new_key_read_i)){
//do not overwrite keymap currently being processed
INCR_NEW_KEY_I(new_key_write_i);
}
NewKeyBuf[new_key_write_i] = new_keys;
INCR_NEW_KEY_I(new_key_write_i);
new_key_empty = 0;
NewKeyEmpty = 0;
}
//track time
#ifdef TRACK_TIME
count++;
if (count == 200){
count = 0;
@ -63,6 +67,7 @@ void timer0_isr() __interrupt 1 __using 1
}
}
}
#endif
}
@ -104,8 +109,15 @@ int main()
uint8_t entering_exp = ENTERING_DONE;
uint8_t no_lift = 0;
uint8_t exp_i = 0;
#ifdef DEBUG_KEYS
uint8_t j = 0;
const uint8_t* keys;
uint8_t key_i;
#endif
#ifdef DEBUG_UPTIME
uint32_t i;
#endif
Timer0Init(); // display refresh & switch read
LCD_Open();
KeyInit();
@ -116,12 +128,17 @@ int main()
P3M1 &= ~(0x4);
P3M0 |= (0x4);
#ifdef DEBUG_UPTIME
i = 0;
j = 0;
#endif
// LOOP
while (1)
{
LCD_GoTo(0,0);
#ifdef DEBUG_UPTIME
u32str(i++, Buf, 10);
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#else
//display y register on first line
if (entering_exp == ENTERING_DONE){
dec80_to_str(Buf, get_y());
@ -130,6 +147,9 @@ int main()
dec80_to_str(Buf, get_x());
}
LCD_OutString(Buf, MAX_CHARS_PER_LINE);
#endif //DEBUG_UPTIME
#ifdef DEBUG_KEYS
//keyboard debug
keys = DebugGetKeys();
for (key_i = 0; key_i < 5; key_i++){
@ -150,14 +170,16 @@ int main()
} else {
LCD_OutString(u32str(SecCount, Buf, 10));
}
#endif //DEBUG_KEYS
///new keys
if (!new_key_empty){
///get new keys
if (!NewKeyEmpty){
uint8_t i_key;
uint32_t new_keys = NewKeyBuf[new_key_read_i];
INCR_NEW_KEY_I(new_key_read_i);
if (new_key_read_i == new_key_write_i){
new_key_empty = 1;
NewKeyEmpty = 1;
}
#ifdef DEBUG_KEYS

View File

@ -5,11 +5,12 @@
void _delay_ms(uint8_t ms){
//TODO:
}
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us)
{
//TODO:
}
#else
#endif
void _delay_ms(uint8_t ms)
{
// delay function, tuned for 11.583 MHz clock
@ -28,6 +29,7 @@ void _delay_ms(uint8_t ms)
__endasm;
}
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us)
{
// delay function, tuned for 11.583 MHz clock

View File

@ -11,7 +11,13 @@
void _delay_ms(uint8_t ms);
#define ACCURATE_DELAY_US
#ifdef ACCURATE_DELAY_US
void _delay_us(uint8_t us);
#else
#define _delay_us(x) _delay_ms(1)
#endif
char* u32str(uint32_t x, char* buf, uint8_t base);