no more cstring; 16mhz/115200 msp430; fix dict init

master
Clyne 1 year ago
parent 69152efdad
commit cdf19490df
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -25,7 +25,6 @@
#include <vector> #include <vector>
#ifdef ALEE_MSP430 #ifdef ALEE_MSP430
#include <cstring>
#include "lzss.h" #include "lzss.h"
static const static const
#include "msp430fr2476_all.h" #include "msp430fr2476_all.h"
@ -190,7 +189,7 @@ Error findword(State& state, Word word)
if (c != LZSS_MAGIC_SEPARATOR) { if (c != LZSS_MAGIC_SEPARATOR) {
*lzptr++ = (char)c; *lzptr++ = (char)c;
} else { } else {
if (lzwlen == lzptr - lzbuf - 2 && strncmp(lzword, lzbuf, lzptr - lzbuf - 2) == 0) { if (lzwlen == lzptr - lzbuf - 2 && std::equal(lzbuf, lzptr - 2, lzword)) {
lzwlen = (*(lzptr - 2) << 8) | *(lzptr - 1); lzwlen = (*(lzptr - 2) << 8) | *(lzptr - 1);
return 1; return 1;
} else { } else {

@ -19,11 +19,10 @@
#ifndef ALEEFORTH_COREWORDS_HPP #ifndef ALEEFORTH_COREWORDS_HPP
#define ALEEFORTH_COREWORDS_HPP #define ALEEFORTH_COREWORDS_HPP
#include "ctype.hpp"
#include "types.hpp" #include "types.hpp"
#include "state.hpp" #include "state.hpp"
#include <cstring>
/** /**
* To be implemented by the user, this function is called when the `sys` word * To be implemented by the user, this function is called when the `sys` word
* is executed. * is executed.
@ -42,7 +41,7 @@ public:
*/ */
static Cell findi(State&, Word); static Cell findi(State&, Word);
consteval static Cell findi(const char *word) { consteval static Cell findi(const char *word) {
return findi(word, std::strlen(word)); return findi(word, strlen(word));
} }
/** /**
@ -67,7 +66,7 @@ private:
const char *ptr = CoreWords::wordsarr; const char *ptr = CoreWords::wordsarr;
for (Cell wordsi = 0; wordsi < WordCount; ++wordsi) { for (Cell wordsi = 0; wordsi < WordCount; ++wordsi) {
std::size_t wordsize = std::strlen(ptr); std::size_t wordsize = strlen(ptr);
if (wordsize == size && Dictionary::equal(ptr, ptr + wordsize, it)) if (wordsize == size && Dictionary::equal(ptr, ptr + wordsize, it))
return wordsi; return wordsi;

@ -25,6 +25,12 @@
#include <cstdint> #include <cstdint>
constexpr inline unsigned strlen(const char * const s) {
unsigned i = 0;
while (s[i]) i++;
return i;
}
constexpr inline bool isspace(uint8_t c) { constexpr inline bool isspace(uint8_t c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; return c == ' ' || c == '\t' || c == '\r' || c == '\n';
} }

@ -18,8 +18,6 @@
#include "dictionary.hpp" #include "dictionary.hpp"
#include <cstring>
void Dictionary::initialize() void Dictionary::initialize()
{ {
write(Base, 10); write(Base, 10);

@ -20,16 +20,13 @@
#include "ctype.hpp" #include "ctype.hpp"
#include "parser.hpp" #include "parser.hpp"
#include <algorithm>
#include <cstring>
Error (*Parser::customParse)(State&, Word) = nullptr; Error (*Parser::customParse)(State&, Word) = nullptr;
Error Parser::parse(State& state, const char *str) Error Parser::parse(State& state, const char *str)
{ {
auto addr = Dictionary::Input; auto addr = Dictionary::Input;
const auto len = static_cast<Cell>(std::strlen(str)); const auto len = static_cast<Cell>(strlen(str));
state.dict.write(addr, 0); state.dict.write(addr, 0);
state.dict.write(Dictionary::SourceLen, len); state.dict.write(Dictionary::SourceLen, len);
@ -95,7 +92,7 @@ Error Parser::parseNumber(State& state, Word word)
++it; ++it;
const auto end = word.end(&state.dict); const auto end = word.end(&state.dict);
for (char c; it != end; ++it) { for (uint8_t c; it != end; ++it) {
c = *it; c = *it;
if (isdigit(c)) { if (isdigit(c)) {

@ -19,7 +19,6 @@
#include "corewords.hpp" #include "corewords.hpp"
#include "state.hpp" #include "state.hpp"
#include <cstring>
#include <iterator> #include <iterator>
bool State::compiling() const bool State::compiling() const

@ -22,7 +22,6 @@
static const static const
#include "msp430fr2476_all.h" #include "msp430fr2476_all.h"
#include <cstring>
#include <msp430.h> #include <msp430.h>
#include "splitmemdictrw.hpp" #include "splitmemdictrw.hpp"
@ -40,9 +39,9 @@ static void initGPIO();
static void initClock(); static void initClock();
static void initUART(); static void initUART();
static void Software_Trim(); static void Software_Trim();
#define MCLK_FREQ_MHZ (8) // MCLK = 8MHz #define MCLK_FREQ_MHZ (16)
#define ALEE_RODICTSIZE (9200) #define ALEE_RODICTSIZE (9400)
__attribute__((section(".lodict"))) __attribute__((section(".lodict")))
#include "core.fth.h" #include "core.fth.h"
@ -50,10 +49,8 @@ static bool exitLpm;
static Addr isr_list[24] = {}; static Addr isr_list[24] = {};
using DictType = SplitMemDictRW<ALEE_RODICTSIZE, 32767>; using DictType = SplitMemDictRW<ALEE_RODICTSIZE, 32767>;
extern char __dict[sizeof(DictType)];
alignas(DictType) static auto& dict = *(new (__dict) DictType (alee_dat, 0x10000));
static unsigned char dictbuf[sizeof(DictType)];
static auto dict = reinterpret_cast<DictType *>(dictbuf);
int main() int main()
{ {
@ -64,9 +61,7 @@ int main()
SYSCFG0 = FRWPPW; SYSCFG0 = FRWPPW;
(void)alee_dat_len; (void)alee_dat_len;
dict->lodict = alee_dat; State state (dict, readchar);
dict->hidict = 0x10000;
State state (*dict, readchar);
Parser::customParse = findword; Parser::customParse = findword;
serputs("alee forth\n\r"); serputs("alee forth\n\r");
@ -203,17 +198,17 @@ void user_sys(State& state)
#define LZSS_MAGIC_SEPARATOR (0xFB) #define LZSS_MAGIC_SEPARATOR (0xFB)
static char lzword[32]; static uint8_t lzword[32];
static int lzwlen; static int lzwlen;
static char lzbuf[32]; static uint8_t lzbuf[32];
static char *lzptr; static uint8_t *lzptr;
Error findword(State& state, Word word) Error findword(State& state, Word word)
{ {
char *ptr = lzword; uint8_t *ptr = lzword;
for (auto it = word.begin(&state.dict); it != word.end(&state.dict); ++it) { for (auto it = word.begin(&state.dict); it != word.end(&state.dict); ++it) {
*ptr = *it; *ptr = *it;
if (islower(*ptr)) if (!isupper(*ptr))
*ptr -= 32; *ptr -= 32;
++ptr; ++ptr;
} }
@ -224,9 +219,9 @@ Error findword(State& state, Word word)
auto ret = decode([](int c) { auto ret = decode([](int c) {
if (c != LZSS_MAGIC_SEPARATOR) { if (c != LZSS_MAGIC_SEPARATOR) {
*lzptr++ = (char)c; *lzptr++ = (uint8_t)c;
} else { } else {
if (lzwlen == lzptr - lzbuf - 2 && strncmp(lzword, lzbuf, lzptr - lzbuf - 2) == 0) { if (lzwlen == lzptr - lzbuf - 2 && std::equal(lzbuf, lzptr - 2, lzword)) {
lzwlen = (*(lzptr - 2) << 8) | *(lzptr - 1); lzwlen = (*(lzptr - 2) << 8) | *(lzptr - 1);
return 1; return 1;
} else { } else {
@ -271,35 +266,47 @@ void initGPIO()
void initClock() void initClock()
{ {
__bis_SR_register(SCG0); // disable FLL static_assert(MCLK_FREQ_MHZ == 16);
CSCTL3 |= SELREF__REFOCLK; // Set REFO as FLL reference source
CSCTL1 = DCOFTRIMEN_1 | DCOFTRIM0 | DCOFTRIM1 | DCORSEL_3;// DCOFTRIM=3, DCO Range = 8MHz // Configure one FRAM waitstate as required by the device datasheet for MCLK
CSCTL2 = FLLD_0 + 243; // DCODIV = 8MHz // operation beyond 8MHz _before_ configuring the clock system.
FRCTL0 = FRCTLPW | NWAITS_1;
P2SEL0 |= BIT0 | BIT1; // P2.0~P2.1: crystal pins
do
{
CSCTL7 &= ~(XT1OFFG | DCOFFG); // Clear XT1 and DCO fault flag
SFRIFG1 &= ~OFIFG;
} while (SFRIFG1 & OFIFG); // Test oscillator fault flag
__bis_SR_register(SCG0); // disable FLL
CSCTL3 |= SELREF__XT1CLK; // Set XT1 as FLL reference source
CSCTL1 = DCOFTRIMEN_1 | DCOFTRIM0 | DCOFTRIM1 | DCORSEL_5;// DCOFTRIM=5, DCO Range = 16MHz
CSCTL2 = FLLD_0 + 487; // DCOCLKDIV = 16MHz
__delay_cycles(3); __delay_cycles(3);
__bic_SR_register(SCG0); // enable FLL __bic_SR_register(SCG0); // enable FLL
Software_Trim(); // Software Trim to get the best DCOFTRIM value Software_Trim(); // Software Trim to get the best DCOFTRIM value
CSCTL4 = SELMS__DCOCLKDIV | SELA__XT1CLK; // set XT1 (~32768Hz) as ACLK source, ACLK = 32768Hz
// default DCOCLKDIV as MCLK and SMCLK source
CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
// default DCODIV as MCLK and SMCLK source
} }
void initUART() void initUART()
{ {
// Configure UART pins // Configure UART pins
P5SEL0 |= BIT1 | BIT2; // set 2-UART pin as second function P5SEL0 |= BIT1 | BIT2;
SYSCFG3|=USCIA0RMP; //Set the remapping source SYSCFG3 |= USCIA0RMP; // Set the remapping source
// Configure UART
UCA0CTLW0 |= UCSWRST; UCA0CTLW0 |= UCSWRST;
UCA0CTLW0 |= UCSSEL__SMCLK; UCA0CTLW0 |= UCSSEL__SMCLK; // 16 MHz
// Baud Rate calculation // Baud Rate calculation
// 8000000/(16*9600) = 52.083 // N = 16MHz / 115200 = 138.888
// Fractional portion = 0.083 // OS16 = 1, UCBRx = INT(N/16) = 8(.6806)
// User's Guide Table 17-4: UCBRSx = 0x49 // UCBRFx = INT( ((N/16) - UCBRx) * 16) = 10(.8896)
// UCBRFx = int ( (52.083-52)*16) = 1 UCA0BRW = 8;
UCA0BR0 = 52; // 8000000/16/9600 UCA0MCTLW = 0xD600 | 0x00A0 | UCOS16;
UCA0BR1 = 0x00;
UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
} }
@ -378,7 +385,7 @@ bool alee_isr_handle(unsigned index)
const Addr isr = isr_list[index]; const Addr isr = isr_list[index];
if (isr != 0) { if (isr != 0) {
State isrstate (*dict, readchar); State isrstate (dict, readchar);
exitLpm = false; exitLpm = false;
isrstate.execute(isr); isrstate.execute(isr);
return exitLpm; return exitLpm;
@ -418,3 +425,6 @@ DEFINE_ISR(TIMER1_A0_VECTOR, 21)
DEFINE_ISR(TIMER0_A1_VECTOR, 22) DEFINE_ISR(TIMER0_A1_VECTOR, 22)
DEFINE_ISR(TIMER0_A0_VECTOR, 23) DEFINE_ISR(TIMER0_A0_VECTOR, 23)
// Override newlib's free to save hundreds of bytes
extern "C" void free(void *) {}

@ -214,7 +214,9 @@ SECTIONS
KEEP (*(.dtors)) KEEP (*(.dtors))
} > FRAM } > FRAM
.tinyram : {} > TINYRAM .tinyram : {
PROVIDE (__dict = .);
} > TINYRAM
.data : .data :
{ {

@ -40,7 +40,6 @@ public:
return *this; return *this;
} }
virtual Cell read(Addr addr) const noexcept final { virtual Cell read(Addr addr) const noexcept final {
if (addr < LON) if (addr < LON)
return *reinterpret_cast<const Cell *>(lodict + addr); return *reinterpret_cast<const Cell *>(lodict + addr);
@ -72,6 +71,9 @@ public:
virtual unsigned long int capacity() const noexcept final { virtual unsigned long int capacity() const noexcept final {
return LON + HIN; return LON + HIN;
} }
private:
virtual ~SplitMemDictRW() override {};
}; };
#endif // ALEEFORTH_SPLITMEMDICTRW_HPP #endif // ALEEFORTH_SPLITMEMDICTRW_HPP

Loading…
Cancel
Save