msp4th: config is passed as a struct
authorDan White <dan@whiteaudio.com>
Fri, 10 May 2013 21:21:33 +0000 (16:21 -0500)
committerDan White <dan@whiteaudio.com>
Fri, 10 May 2013 21:21:33 +0000 (16:21 -0500)
No hard-coded locations for passing config pointers.  This cleans up the linker
script.

msp4th/ldscript_ns430
msp4th/ldscript_ns430_bootrom
msp4th/main.c
msp4th/msp4th.c
msp4th/msp4th.h
msp4th/test4th.c

index 4345e834dc0b46366d4a7f6c3325927d65793982..accb6c8fb8df8672ddeb90216f2893357e05d803 100644 (file)
@@ -6,7 +6,7 @@ MEMORY {
   peripheral_8bit  : ORIGIN = 0x0010, LENGTH = 0x00f0
   peripheral_16bit : ORIGIN = 0x0100, LENGTH = 0x0100
 
-  ram (wx)         : ORIGIN = 0x4000, LENGTH = 0xbf00
+  ram (wx)         : ORIGIN = 0x4000, LENGTH = 0xbfc0
   /*rom (rx)         : ORIGIN = 0x3000, LENGTH = 0x1000 */
 
   vectors          : ORIGIN = 0xffc0, LENGTH = 64
@@ -218,17 +218,6 @@ SECTIONS
    PROVIDE (__data_start_rom = _etext);
    PROVIDE (__data_end_rom   = _etext + SIZEOF (.data));
 }
-/* hard-coded addresses for on-line re-sizing of the msp4th arrays */
-__msp4th_mathStackStartAddress     = 0xff00;
-__msp4th_addrStackStartAddress     = 0xff02;
-__msp4th_prog                      = 0xff04;
-__msp4th_progOpcodes               = 0xff06;
-__msp4th_cmdList                   = 0xff08;
-__msp4th_lineBuffer                = 0xff0a;
-__msp4th_lineBufferLength          = 0xff0c;
-__msp4th_wordBuffer                = 0xff0e;
-__msp4th_wordBufferLength          = 0xff10;
-
 __PC_AFTER_RESET = 0x3000;
 __CRCDI = 0x0000;
 __CRCDI_L = 0x0000;
index 22c15e35ce919f59f7928c9f66c0d4e198ca5aff..ae03a2aa460d11a6823c40c6c14bb96b4ab8fa51 100644 (file)
@@ -6,7 +6,7 @@ MEMORY {
   peripheral_8bit  : ORIGIN = 0x0010, LENGTH = 0x00f0
   peripheral_16bit : ORIGIN = 0x0100, LENGTH = 0x0100
 
-  ram (wx)         : ORIGIN = 0x4000, LENGTH = 0xbf00
+  ram (wx)         : ORIGIN = 0x4000, LENGTH = 0xbfc0
   rom (rx)         : ORIGIN = 0x3000, LENGTH = 0x1000
 
   vectors          : ORIGIN = 0xffc0, LENGTH = 64
@@ -218,17 +218,6 @@ SECTIONS
    PROVIDE (__data_start_rom = _etext);
    PROVIDE (__data_end_rom   = _etext + SIZEOF (.data));
 }
-/* hard-coded addresses for on-line re-sizing of the msp4th arrays */
-__msp4th_mathStackStartAddress     = 0xff00;
-__msp4th_addrStackStartAddress     = 0xff02;
-__msp4th_prog                      = 0xff04;
-__msp4th_progOpcodes               = 0xff06;
-__msp4th_cmdList                   = 0xff08;
-__msp4th_lineBuffer                = 0xff0a;
-__msp4th_lineBufferLength          = 0xff0c;
-__msp4th_wordBuffer                = 0xff0e;
-__msp4th_wordBufferLength          = 0xff10;
-
 __PC_AFTER_RESET = 0x3000;
 __CRCDI = 0x0000;
 __CRCDI_L = 0x0000;
index afea0740609fad505d3f940ae25399c1a8b57dac..11f50c979692f51bd4ac935f307d89066d886cc1 100644 (file)
@@ -118,34 +118,25 @@ uint8_t __attribute__ ((section(".noinit"))) cmdListArray[USER_CMD_LIST_SIZE];
 uint8_t __attribute__ ((section(".noinit"))) lineBufferArray[LINE_BUFFER_SIZE];
 uint8_t __attribute__ ((section(".noinit"))) wordBufferArray[WORD_BUFFER_SIZE];
 
-void (*msp4th_putchar)(uint8_t);
-uint8_t (*msp4th_getchar)(void);
+struct msp4th_config __attribute__ ((section(".noinit"))) default_config;
 
 
-static __inline__ void config_default_msp4th(void)
+static __inline__ void setup_default_msp4th(void)
 {
-    int16_t i;
-
-    msp4th_mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
-    msp4th_addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
-    msp4th_prog = &progArray[0];
-    msp4th_progOpcodes = &progOpcodesArray[0];
-    msp4th_cmdList = &cmdListArray[0];
-    msp4th_lineBuffer = &lineBufferArray[0];
-    msp4th_lineBufferLength = LINE_BUFFER_SIZE;
-    msp4th_wordBuffer = &wordBufferArray[0];
-    msp4th_wordBufferLength = WORD_BUFFER_SIZE;
-
-
-    for (i=0; i < MATH_STACK_SIZE; i++) {
-        mathStackArray[i] = 0;
-    }
-
-    for (i=0; i < ADDR_STACK_SIZE; i++) {
-        addrStackArray[i] = 0;
-    }
-
-    /* example initial line to execute
+    default_config.mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
+    default_config.addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
+    default_config.prog = &progArray[0];
+    default_config.progOpcodes = &progOpcodesArray[0];
+    default_config.cmdList = &cmdListArray[0];
+    default_config.lineBuffer = &lineBufferArray[0];
+    default_config.lineBufferLength = LINE_BUFFER_SIZE;
+    default_config.wordBuffer = &wordBufferArray[0];
+    default_config.wordBufferLength = WORD_BUFFER_SIZE;
+    default_config.putchar = &uart_putchar;
+    default_config.getchar = &uart_getchar;
+
+
+    /* howto execute a line of words on init
     uint8_t *str = (uint8_t *)"1 2 3 4 5 s.\r";
     for (i=0; i < 14; i++) {
         lineBufferArray[i] = str[i];
@@ -155,9 +146,6 @@ static __inline__ void config_default_msp4th(void)
 
     lineBufferArray[0] = 0;
     wordBufferArray[0] = 0;
-
-    msp4th_putchar = &uart_putchar;
-    msp4th_getchar = &uart_getchar;
 }
 
 
@@ -205,9 +193,10 @@ int main(void){
      *  - any 0xff character in the input
      */
     while (1) {
-        config_default_msp4th();
-        init_msp4th();
-        processLoop();
+        setup_default_msp4th();
+
+        msp4th_init(&default_config);
+        msp4th_processLoop();
     }
 
     return 0;
index 0edb45c468e9f7e88859443ae78cbb5438096bf5..afcfeff3844e168af484a82a9f9183b69022724a 100644 (file)
@@ -331,21 +331,28 @@ int16_t cmdListIdx;    // next open space for user word strings
  */
 #if defined(MSP430)
 int16_t register *mathStackPtr asm("r6");
-#else
-int16_t *mathStackPtr;
-#endif
-
-#if defined(MSP430)
 int16_t register *addrStackPtr asm("r7");
 #else
+int16_t *mathStackPtr;
 int16_t *addrStackPtr;
 #endif
 
 int16_t *prog;          // user programs (opcodes) are placed here
 
-//int16_t *progOpcodes;   // mapping between user word index and program opcodes
+int16_t *progOpcodes;   // mapping between user word index and program opcodes
                         // start index into prog[]
 
+uint8_t *cmdList;
+uint8_t *lineBuffer;
+int16_t lineBufferLength;
+uint8_t *wordBuffer;
+int16_t wordBufferLength;
+void (*msp4th_putchar)(uint8_t c);
+uint8_t (*msp4th_getchar)(void);
+
+struct msp4th_config *config;
+
+
 
 
 
@@ -372,10 +379,10 @@ uint8_t getKeyB()
 {
     uint8_t c;
 
-    c = msp4th_lineBuffer[lineBufferIdx++];
+    c = lineBuffer[lineBufferIdx++];
     if (c == 0) {
         getLine();
-        c = msp4th_lineBuffer[lineBufferIdx++];
+        c = lineBuffer[lineBufferIdx++];
     }
 
     return (c);
@@ -407,8 +414,8 @@ void getLine()
         } else if ( ((c == 255) || (c == '\ 4')) && (lineBufferIdx == 0)) {
             xit = 1;
             waiting = 0;
-            msp4th_lineBuffer[lineBufferIdx++] = 'x';
-            msp4th_lineBuffer[lineBufferIdx] = ' ';
+            lineBuffer[lineBufferIdx++] = 'x';
+            lineBuffer[lineBufferIdx] = ' ';
         } else {
             if (echo) {
                 msp4th_putchar(c);
@@ -416,15 +423,15 @@ void getLine()
 
             if ( (c == '\r') ||
                  (c == '\n') ||
-                 (lineBufferIdx >= (msp4th_lineBufferLength - 1))) { // prevent overflow of line buffer
+                 (lineBufferIdx >= (lineBufferLength - 1))) { // prevent overflow of line buffer
 
                 waiting = 0;
 
                 if (echo) { msp4th_putchar('\n'); }
             }
 
-            msp4th_lineBuffer[lineBufferIdx++] = c;
-            msp4th_lineBuffer[lineBufferIdx] = 0;
+            lineBuffer[lineBufferIdx++] = c;
+            lineBuffer[lineBufferIdx] = 0;
         }
     }
 
@@ -486,10 +493,10 @@ void getWord(void)
     }
 
     do {
-        msp4th_wordBuffer[k++] = c;
-        msp4th_wordBuffer[k] = 0;
+        wordBuffer[k++] = c;
+        wordBuffer[k] = 0;
         c = getKeyB();
-    } while ((c > ' ') && (k < msp4th_wordBufferLength));
+    } while ((c > ' ') && (k < wordBufferLength));
 }
 
 
@@ -497,7 +504,7 @@ void listFunction()
 {
     msp4th_puts((uint8_t *)cmdListBi);
     msp4th_puts((uint8_t *)cmdListBi2);
-    msp4th_puts((uint8_t *)msp4th_cmdList);
+    msp4th_puts((uint8_t *)cmdList);
 }
 
 
@@ -512,7 +519,7 @@ int16_t popMathStack(void)
     i = *mathStackPtr;
 
     // prevent stack under-flow
-    if (mathStackPtr < (int16_t *)msp4th_mathStackStartAddress) {
+    if (mathStackPtr < (int16_t *)config->mathStackStartAddress) {
         mathStackPtr++;
     }
 
@@ -611,7 +618,7 @@ void luFunc()
 {
   int16_t i;
   
-  i = lookupToken(msp4th_wordBuffer, (uint8_t *)cmdListBi);
+  i = lookupToken(wordBuffer, (uint8_t *)cmdListBi);
   
   if(i){
     i += 20000;
@@ -619,13 +626,13 @@ void luFunc()
     pushMathStack(1);
   } else {
     // need to test internal interp commands
-    i = lookupToken(msp4th_wordBuffer, (uint8_t *)cmdListBi2);
+    i = lookupToken(wordBuffer, (uint8_t *)cmdListBi2);
     if(i){
       i += 10000;
       pushMathStack(i);
       pushMathStack(1);
     } else {
-      i = lookupToken(msp4th_wordBuffer, msp4th_cmdList);
+      i = lookupToken(wordBuffer, cmdList);
       if(i){
         pushMathStack(i);
         pushMathStack(1);
@@ -648,26 +655,26 @@ void numFunc()
     n = 0;
 
     // first check for neg sign
-    if (msp4th_wordBuffer[i] == '-') {
+    if (wordBuffer[i] == '-') {
         i = i + 1;
     }
 
-    if ((msp4th_wordBuffer[i] >= '0') && (msp4th_wordBuffer[i] <= '9')) {
+    if ((wordBuffer[i] >= '0') && (wordBuffer[i] <= '9')) {
         // it is a number 
         isnum = 1;
         // check if hex
-        if(msp4th_wordBuffer[0] == '0' && msp4th_wordBuffer[1] == 'x'){
+        if(wordBuffer[0] == '0' && wordBuffer[1] == 'x'){
             // base 16 number ... just assume all characters are good
             i = 2;
             n = 0;
-            while(msp4th_wordBuffer[i]){
+            while(wordBuffer[i]){
                 n = n << 4;
-                n = n + msp4th_wordBuffer[i] - '0';
-                if(msp4th_wordBuffer[i] > '9'){
+                n = n + wordBuffer[i] - '0';
+                if(wordBuffer[i] > '9'){
                     n = n - 7;
 
                     // compensate for lowercase digits
-                    if (msp4th_wordBuffer[i] >= 'a') {
+                    if (wordBuffer[i] >= 'a') {
                         n -= 0x20;
                     }
                 }
@@ -676,12 +683,12 @@ void numFunc()
         } else {
             // base 10 number
             n = 0;
-            while(msp4th_wordBuffer[i]){
+            while(wordBuffer[i]){
                 n = n * 10;
-                n = n + msp4th_wordBuffer[i] - '0';
+                n = n + wordBuffer[i] - '0';
                 i = i + 1;
             }
-            if(msp4th_wordBuffer[0] == '-'){
+            if(wordBuffer[0] == '-'){
                 n = -n;
             }
         }
@@ -779,14 +786,14 @@ void dfnFunc(){
   uint16_t i;
   // this function adds a new def to the list and creats a new opcode
   i = 0;
-  while(msp4th_wordBuffer[i]){
-    msp4th_cmdList[cmdListIdx++] = msp4th_wordBuffer[i];
+  while(wordBuffer[i]){
+    cmdList[cmdListIdx++] = wordBuffer[i];
     i = i + 1;
   }
-  msp4th_cmdList[cmdListIdx++] = ' ';
-  msp4th_cmdList[cmdListIdx] = 0;
-  i = lookupToken(msp4th_wordBuffer, msp4th_cmdList);
-  msp4th_progOpcodes[i] = progIdx;
+  cmdList[cmdListIdx++] = ' ';
+  cmdList[cmdListIdx] = 0;
+  i = lookupToken(wordBuffer, cmdList);
+  progOpcodes[i] = progIdx;
 }
 
 
@@ -860,7 +867,7 @@ void execFunc(){
   } else {
 
     pushAddrStack(progCounter);
-    progCounter = msp4th_progOpcodes[opcode];
+    progCounter = progOpcodes[opcode];
 
   }
 
@@ -1024,7 +1031,7 @@ void execN(int16_t opcode){
       break;    
 
     case 27: // depth  ( -- n ) \ math stack depth
-      pushMathStack((int16_t *)msp4th_mathStackStartAddress - mathStackPtr);
+      pushMathStack((int16_t *)config->mathStackStartAddress - mathStackPtr);
       break;
       
     case 28: // .h  ( a -- )
@@ -1068,7 +1075,7 @@ void execN(int16_t opcode){
       break;
 
     case 38: // pwrd  ( -- ) \ print word buffer
-      msp4th_puts(msp4th_wordBuffer);
+      msp4th_puts(wordBuffer);
       break;
 
     case 39: // emit  ( c -- )
@@ -1281,7 +1288,7 @@ void execN(int16_t opcode){
     case 72: // s.  ( -- ) \ print stack contents, TOS on right
       { // addr is strictly local to this block
           int16_t *addr;
-          addr = (int16_t *)msp4th_mathStackStartAddress;
+          addr = (int16_t *)config->mathStackStartAddress;
           while (addr >= mathStackPtr) {
               printNumber(*addr);
               addr--;
@@ -1292,7 +1299,7 @@ void execN(int16_t opcode){
     case 73: // sh.  ( -- ) \ print stack contents in hex, TOS on right
       { // addr is strictly local to this block
           int16_t *addr;
-          addr = (int16_t *)msp4th_mathStackStartAddress;
+          addr = (int16_t *)config->mathStackStartAddress;
           while (addr >= mathStackPtr) {
               printHexWord(*addr);
               msp4th_putchar(' ');
@@ -1324,17 +1331,28 @@ void execN(int16_t opcode){
  * Public function prototypes
  */
 
-void init_msp4th(void)
+void msp4th_init(struct msp4th_config *c)
 {
     /*
      * Get addresses of user-configurable arrays from the pre-known vector
      * table locations.
      *
-     * Changing the values in the xStartAddress locations and calling
+     * Changing the values in the msp4th_* locations and calling
      * init_msp4th() again restarts the interpreter with the new layout;
      */
-    mathStackPtr = msp4th_mathStackStartAddress;
-    addrStackPtr = msp4th_addrStackStartAddress;
+    mathStackPtr = c->mathStackStartAddress;
+    addrStackPtr = c->addrStackStartAddress;
+    prog = c->prog;
+    progOpcodes = c->progOpcodes;
+    cmdList = c->cmdList;
+    lineBuffer = c->lineBuffer;
+    lineBufferLength = c->lineBufferLength;
+    wordBuffer = c->wordBuffer;
+    wordBufferLength = c->wordBufferLength;
+    msp4th_putchar = c->putchar;
+    msp4th_getchar = c->getchar;
+
+    config = c;
 
 
     xit = 0;
@@ -1348,7 +1366,7 @@ void init_msp4th(void)
 }
 
 
-void processLoop() // this processes the forth opcodes.
+void msp4th_processLoop() // this processes the forth opcodes.
 {
     uint16_t opcode;
     uint16_t tmp;
@@ -1368,7 +1386,7 @@ void processLoop() // this processes the forth opcodes.
             execN(opcode - 20000);
         } else {
             pushAddrStack(progCounter);
-            progCounter = msp4th_progOpcodes[opcode];
+            progCounter = progOpcodes[opcode];
         }
     } // while ()
 }
index 039bd86d5a21923133238a1253fa9327017f2715..ce777a353a2ac44f7e8bbb1c57a180500e0c57d4 100644 (file)
@@ -1,36 +1,26 @@
 #if !defined(__MSP4TH)
 #define __MSP4TH
 
-#if defined(MSP430)
-extern int16_t *msp4th_mathStackStartAddress asm("__msp4th_mathStackStartAddress");
-extern int16_t *msp4th_addrStackStartAddress asm("__msp4th_addrStackStartAddress");
-extern int16_t *msp4th_prog asm("__msp4th_prog");
-extern int16_t *msp4th_progOpcodes asm("__msp4th_progOpcodes");
-extern uint8_t *msp4th_cmdList asm("__msp4th_cmdList");
-extern uint8_t *msp4th_lineBuffer asm("__msp4th_lineBuffer");
-extern int16_t msp4th_lineBufferLength asm("__msp4th_lineBufferLength");
-extern uint8_t *msp4th_wordBuffer asm("__msp4th_wordBuffer");
-extern int16_t msp4th_wordBufferLength asm("__msp4th_wordBufferLength");
-#else
-extern int16_t *msp4th_mathStackStartAddress;
-extern int16_t *msp4th_addrStackStartAddress;
-extern int16_t *msp4th_prog;
-extern int16_t *msp4th_progOpcodes;
-extern uint8_t *msp4th_cmdList;
-extern uint8_t *msp4th_lineBuffer;
-extern int16_t msp4th_lineBufferLength;
-extern uint8_t *msp4th_wordBuffer;
-extern int16_t msp4th_wordBufferLength;
-#endif
-
 /*
- * Provide definitions for these functions in user code and assign the function
- * pointer before calling init_msp4th()
+ * Provide definitions for these arrays in user code and assign the
+ * pointers, then call msp4th_init(&config).
  */
-extern void (*msp4th_putchar)(uint8_t c);
-extern uint8_t (*msp4th_getchar)(void);
 
-void init_msp4th(void);
-void processLoop(void);
+struct msp4th_config {
+    int16_t *mathStackStartAddress;
+    int16_t *addrStackStartAddress;
+    int16_t *prog;
+    int16_t *progOpcodes;
+    uint8_t *cmdList;
+    uint8_t *lineBuffer;
+    int16_t lineBufferLength;
+    uint8_t *wordBuffer;
+    int16_t wordBufferLength;
+    void (*putchar)(uint8_t);
+    uint8_t (*getchar)(void);
+};
+
+void msp4th_init(struct msp4th_config *);
+void msp4th_processLoop(void);
 
 #endif
index 769558a57b4b63c4bda19c4d98fac5be7c9aef20..8c8a7ef6ed3c99cd2970fb653d20cc65d3e4ce3b 100644 (file)
@@ -27,6 +27,9 @@ uint8_t cmdListArray[CMD_LIST_SIZE];
 uint8_t lineBufferArray[CMD_LIST_SIZE];
 uint8_t wordBufferArray[CMD_LIST_SIZE];
 
+struct msp4th_config default_config;
+
+/*
 int16_t *msp4th_mathStackStartAddress;
 int16_t *msp4th_addrStackStartAddress;
 int16_t *msp4th_prog;
@@ -36,9 +39,10 @@ uint8_t *msp4th_lineBuffer;
 int16_t msp4th_lineBufferLength;
 uint8_t *msp4th_wordBuffer;
 int16_t msp4th_wordBufferLength;
+*/
 
-void (*msp4th_putchar)(uint8_t);
-uint8_t (*msp4th_getchar)(void);
+//void (*msp4th_putchar)(uint8_t);
+//uint8_t (*msp4th_getchar)(void);
 
 
 
@@ -60,6 +64,19 @@ void config_msp4th(void)
 {
     int16_t i;
 
+    default_config.mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
+    default_config.addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
+    default_config.prog = &progArray[0];
+    default_config.progOpcodes = &progOpcodesArray[0];
+    default_config.cmdList = &cmdListArray[0];
+    default_config.lineBuffer = &lineBufferArray[0];
+    default_config.lineBufferLength = LINE_BUFFER_SIZE;
+    default_config.wordBuffer = &wordBufferArray[0];
+    default_config.wordBufferLength = WORD_BUFFER_SIZE;
+    default_config.putchar = &my_putchar;
+    default_config.getchar = &my_getchar;
+
+    /*
     msp4th_mathStackStartAddress = &mathStackArray[MATH_STACK_SIZE - 1];
     msp4th_addrStackStartAddress = &addrStackArray[ADDR_STACK_SIZE - 1];
 
@@ -75,6 +92,10 @@ void config_msp4th(void)
     msp4th_wordBuffer = &wordBufferArray[0];
     msp4th_wordBufferLength = WORD_BUFFER_SIZE;
 
+    msp4th_putchar = &my_putchar;
+    msp4th_getchar = &my_getchar;
+    */
+
 
     for (i=0; i < MATH_STACK_SIZE; i++) {
         mathStackArray[i] = 0;
@@ -86,9 +107,6 @@ void config_msp4th(void)
 
     lineBufferArray[0] = 0;
     wordBufferArray[0] = 0;
-
-    msp4th_putchar = &my_putchar;
-    msp4th_getchar = &my_getchar;
 }
 
 
@@ -98,8 +116,9 @@ int main(void)
 
     while (1) {
         config_msp4th();
-        init_msp4th();
-        processLoop();
+
+        msp4th_init(&default_config);
+        msp4th_processLoop();
     }
 
     return 0;