Skip to content

Commit

Permalink
no more cstring; 16mhz/115200 msp430; fix dict init
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Nov 12, 2023
1 parent 69152ef commit cdf1949
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 53 deletions.
3 changes: 1 addition & 2 deletions alee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <vector>

#ifdef ALEE_MSP430
#include <cstring>
#include "lzss.h"
static const
#include "msp430fr2476_all.h"
Expand Down Expand Up @@ -190,7 +189,7 @@ Error findword(State& state, Word word)
if (c != LZSS_MAGIC_SEPARATOR) {
*lzptr++ = (char)c;
} 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);
return 1;
} else {
Expand Down
7 changes: 3 additions & 4 deletions libalee/corewords.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
#ifndef ALEEFORTH_COREWORDS_HPP
#define ALEEFORTH_COREWORDS_HPP

#include "ctype.hpp"
#include "types.hpp"
#include "state.hpp"

#include <cstring>

/**
* To be implemented by the user, this function is called when the `sys` word
* is executed.
Expand All @@ -42,7 +41,7 @@ class CoreWords
*/
static Cell findi(State&, Word);
consteval static Cell findi(const char *word) {
return findi(word, std::strlen(word));
return findi(word, strlen(word));
}

/**
Expand All @@ -67,7 +66,7 @@ class CoreWords
const char *ptr = CoreWords::wordsarr;

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))
return wordsi;
Expand Down
6 changes: 6 additions & 0 deletions libalee/ctype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

#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) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
Expand Down
2 changes: 0 additions & 2 deletions libalee/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include "dictionary.hpp"

#include <cstring>

void Dictionary::initialize()
{
write(Base, 10);
Expand Down
7 changes: 2 additions & 5 deletions libalee/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
#include "ctype.hpp"
#include "parser.hpp"

#include <algorithm>
#include <cstring>

Error (*Parser::customParse)(State&, Word) = nullptr;

Error Parser::parse(State& state, const char *str)
{
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(Dictionary::SourceLen, len);

Expand Down Expand Up @@ -95,7 +92,7 @@ Error Parser::parseNumber(State& state, Word word)
++it;

const auto end = word.end(&state.dict);
for (char c; it != end; ++it) {
for (uint8_t c; it != end; ++it) {
c = *it;

if (isdigit(c)) {
Expand Down
1 change: 0 additions & 1 deletion libalee/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "corewords.hpp"
#include "state.hpp"

#include <cstring>
#include <iterator>

bool State::compiling() const
Expand Down
84 changes: 47 additions & 37 deletions msp430/alee-msp430.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
static const
#include "msp430fr2476_all.h"

#include <cstring>
#include <msp430.h>

#include "splitmemdictrw.hpp"
Expand All @@ -40,20 +39,18 @@ static void initGPIO();
static void initClock();
static void initUART();
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")))
#include "core.fth.h"

static bool exitLpm;
static Addr isr_list[24] = {};

using DictType = SplitMemDictRW<ALEE_RODICTSIZE, 32767>;

alignas(DictType)
static unsigned char dictbuf[sizeof(DictType)];
static auto dict = reinterpret_cast<DictType *>(dictbuf);
extern char __dict[sizeof(DictType)];
static auto& dict = *(new (__dict) DictType (alee_dat, 0x10000));

int main()
{
Expand All @@ -64,9 +61,7 @@ int main()
SYSCFG0 = FRWPPW;

(void)alee_dat_len;
dict->lodict = alee_dat;
dict->hidict = 0x10000;
State state (*dict, readchar);
State state (dict, readchar);
Parser::customParse = findword;

serputs("alee forth\n\r");
Expand Down Expand Up @@ -203,17 +198,17 @@ void user_sys(State& state)

#define LZSS_MAGIC_SEPARATOR (0xFB)

static char lzword[32];
static uint8_t lzword[32];
static int lzwlen;
static char lzbuf[32];
static char *lzptr;
static uint8_t lzbuf[32];
static uint8_t *lzptr;

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) {
*ptr = *it;
if (islower(*ptr))
if (!isupper(*ptr))
*ptr -= 32;
++ptr;
}
Expand All @@ -224,9 +219,9 @@ Error findword(State& state, Word word)

auto ret = decode([](int c) {
if (c != LZSS_MAGIC_SEPARATOR) {
*lzptr++ = (char)c;
*lzptr++ = (uint8_t)c;
} 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);
return 1;
} else {
Expand Down Expand Up @@ -271,35 +266,47 @@ void initGPIO()

void initClock()
{
__bis_SR_register(SCG0); // disable FLL
CSCTL3 |= SELREF__REFOCLK; // Set REFO as FLL reference source
CSCTL1 = DCOFTRIMEN_1 | DCOFTRIM0 | DCOFTRIM1 | DCORSEL_3;// DCOFTRIM=3, DCO Range = 8MHz
CSCTL2 = FLLD_0 + 243; // DCODIV = 8MHz
static_assert(MCLK_FREQ_MHZ == 16);

// Configure one FRAM waitstate as required by the device datasheet for MCLK
// 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);
__bic_SR_register(SCG0); // enable FLL
Software_Trim(); // Software Trim to get the best DCOFTRIM value
__bic_SR_register(SCG0); // enable FLL
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()
{
// Configure UART pins
P5SEL0 |= BIT1 | BIT2; // set 2-UART pin as second function
SYSCFG3|=USCIA0RMP; //Set the remapping source
// Configure UART
P5SEL0 |= BIT1 | BIT2;
SYSCFG3 |= USCIA0RMP; // Set the remapping source

UCA0CTLW0 |= UCSWRST;
UCA0CTLW0 |= UCSSEL__SMCLK;
UCA0CTLW0 |= UCSSEL__SMCLK; // 16 MHz

// Baud Rate calculation
// 8000000/(16*9600) = 52.083
// Fractional portion = 0.083
// User's Guide Table 17-4: UCBRSx = 0x49
// UCBRFx = int ( (52.083-52)*16) = 1
UCA0BR0 = 52; // 8000000/16/9600
UCA0BR1 = 0x00;
UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;
// N = 16MHz / 115200 = 138.888
// OS16 = 1, UCBRx = INT(N/16) = 8(.6806)
// UCBRFx = INT( ((N/16) - UCBRx) * 16) = 10(.8896)
UCA0BRW = 8;
UCA0MCTLW = 0xD600 | 0x00A0 | UCOS16;

UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
}
Expand Down Expand Up @@ -378,7 +385,7 @@ bool alee_isr_handle(unsigned index)
const Addr isr = isr_list[index];

if (isr != 0) {
State isrstate (*dict, readchar);
State isrstate (dict, readchar);
exitLpm = false;
isrstate.execute(isr);
return exitLpm;
Expand Down Expand Up @@ -418,3 +425,6 @@ DEFINE_ISR(TIMER1_A0_VECTOR, 21)
DEFINE_ISR(TIMER0_A1_VECTOR, 22)
DEFINE_ISR(TIMER0_A0_VECTOR, 23)

// Override newlib's free to save hundreds of bytes
extern "C" void free(void *) {}

4 changes: 3 additions & 1 deletion msp430/msp430fr2476.ld
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ SECTIONS
KEEP (*(.dtors))
} > FRAM

.tinyram : {} > TINYRAM
.tinyram : {
PROVIDE (__dict = .);
} > TINYRAM

.data :
{
Expand Down
4 changes: 3 additions & 1 deletion splitmemdictrw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class SplitMemDictRW : public Dictionary
return *this;
}


virtual Cell read(Addr addr) const noexcept final {
if (addr < LON)
return *reinterpret_cast<const Cell *>(lodict + addr);
Expand Down Expand Up @@ -72,6 +71,9 @@ class SplitMemDictRW : public Dictionary
virtual unsigned long int capacity() const noexcept final {
return LON + HIN;
}

private:
virtual ~SplitMemDictRW() override {};
};

#endif // ALEEFORTH_SPLITMEMDICTRW_HPP
Expand Down

0 comments on commit cdf1949

Please sign in to comment.