Skip to content

Commit

Permalink
chg: Code refactor - prepare to use abstract canvas in parts !minor
Browse files Browse the repository at this point in the history
  • Loading branch information
lcgamboa committed Jan 29, 2024
1 parent e1f903b commit 01f111e
Show file tree
Hide file tree
Showing 190 changed files with 5,161 additions and 3,135 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ PACKAGE=picsimlab
MAINVER=0
MINORVER=9
VERSION=0.9.1
DATE=240126
DATE=240128
VERSION_STABLE=0.9.1
99 changes: 69 additions & 30 deletions src/boards/board_Arduino_Uno.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ void cboard_Arduino_Uno::EvMouseButtonRelease(uint button, uint x, uint y, uint
// Called ever 100ms to draw board
// This is the critical code for simulator running speed

void cboard_Arduino_Uno::Draw(CCanvas* Canvas) {
void cboard_Arduino_Uno::Draw(void) {
int i;
int update = 0; // verifiy if updated is needed

Expand All @@ -311,52 +311,62 @@ void cboard_Arduino_Uno::Draw(CCanvas* Canvas) {
output[i].update = 0;

if (!update) {
Canvas->Init(Scale, Scale);
Canvas->SetFontWeight(lxFONTWEIGHT_BOLD);
PICSimLab.CanvasCmd({CC_INIT, .Init{Scale, Scale, 0}});
PICSimLab.CanvasCmd({CC_SETFONTWEIGHT, .SetFontWeight{lxFONTWEIGHT_BOLD}});
}
update++; // set to update buffer
if (!output[i].r) // if output shape is a rectangle
{
switch (output[i].id) {
case O_ON:
Canvas->SetColor(0, 200 * PICSimLab.GetMcuPwr() + 55, 0);
PICSimLab.CanvasCmd(
{CC_SETCOLOR, .SetColor{0, (unsigned int)(200 * PICSimLab.GetMcuPwr() + 55), 0}});
break;
case O_RX:
Canvas->SetColor(0, (255 - pins[1].oavalue) * PICSimLab.GetMcuPwr(), 0);
PICSimLab.CanvasCmd(
{CC_SETCOLOR,
.SetColor{0, (unsigned int)(255 - pins[1].oavalue) * PICSimLab.GetMcuPwr(), 0}});
break;
case O_TX:
Canvas->SetColor(0, (255 - ((unsigned char)pins[2].oavalue * 10)) * PICSimLab.GetMcuPwr(), 0);
PICSimLab.CanvasCmd(
{CC_SETCOLOR,
.SetColor{
0, (unsigned int)(255 - ((unsigned char)pins[2].oavalue * 10)) * PICSimLab.GetMcuPwr(),
0}});
break;
case O_L:
Canvas->SetColor(0, pins[LED_pin].oavalue, 0);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{0, (unsigned int)pins[LED_pin].oavalue, 0}});
break;
case O_RST:
Canvas->SetColor(100, 100, 100);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{100, 100, 100}});
break;
default:
Canvas->SetColor(0, 0, 0);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{0, 0, 0}});
break;
}

if (output[i].id == O_RST) {
Canvas->Circle(1, output[i].cx, output[i].cy, ((output[i].x2 - output[i].x1) / 2) - 5);
PICSimLab.CanvasCmd(
{CC_CIRCLE, .Circle{1, output[i].cx, output[i].cy, ((output[i].x2 - output[i].x1) / 2) - 5}});
if (p_RST) {
Canvas->SetColor(15, 15, 15);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{15, 15, 15}});
} else {
Canvas->SetColor(55, 55, 55);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{55, 55, 55}});
}
Canvas->Circle(1, output[i].cx, output[i].cy, ((output[i].x2 - output[i].x1) / 2) - 7);
PICSimLab.CanvasCmd(
{CC_CIRCLE, .Circle{1, output[i].cx, output[i].cy, ((output[i].x2 - output[i].x1) / 2) - 7}});
} else {
Canvas->Rectangle(1, output[i].x1, output[i].y1, output[i].x2 - output[i].x1,
output[i].y2 - output[i].y1);
PICSimLab.CanvasCmd(
{CC_RECTANGLE, .Rectangle{1, output[i].x1, output[i].y1, output[i].x2 - output[i].x1,
output[i].y2 - output[i].y1}});
}
}
}
}

// end draw
if (update) {
Canvas->End();
PICSimLab.CanvasCmd({CC_END});
}

int value = (pins[PWM_pins[0]].oavalue - 55) / 2;
Expand All @@ -380,9 +390,16 @@ void cboard_Arduino_Uno::Run_CPU(void) {
const picpin* pins;
unsigned int alm[100];

// int JUMPSTEPS = Window1.GetJUMPSTEPS ()*4.0; //number of steps skipped
// int JUMPSTEPS =
// Window1.GetJUMPSTEPS
// ()*4.0; //number of steps
// skipped
const int pinc = MGetPinCount();
const long int NSTEP = 4.0 * PICSimLab.GetNSTEP(); // number of steps in 100ms
const long int NSTEP = 4.0 * PICSimLab.GetNSTEP(); // number
// of
// steps
// in
// 100ms
const float RNSTEP = 200.0 * pinc / NSTEP;

long long unsigned int cycle_start;
Expand All @@ -392,19 +409,29 @@ void cboard_Arduino_Uno::Run_CPU(void) {

memset(alm, 0, pinc * sizeof(unsigned int));

// read pic.pins to a local variable to speed up
// read pic.pins to a local
// variable to speed up

pins = MGetPinsValues();

if (use_spare)
SpareParts.PreProcess();

// j = JUMPSTEPS; //step counter
// j = JUMPSTEPS; //step
// counter
pi = 0;
if (PICSimLab.GetMcuPwr()) // if powered
for (i = 0; i < NSTEP; i++) // repeat for number of steps in 100ms
if (PICSimLab.GetMcuPwr()) // if
// powered
for (i = 0; i < NSTEP; i++) // repeat for
// number of
// steps in
// 100ms
{
// verify if a breakpoint is reached if not run one instruction
// verify if a
// breakpoint is
// reached if not
// run one
// instruction
if (avr_debug_type || (!mplabxd_testbp())) {
if (twostep) {
twostep = 0; // NOP
Expand All @@ -427,21 +454,32 @@ void cboard_Arduino_Uno::Run_CPU(void) {
SpareParts.Process();
ioupdated = 0;

// increment mean value counter if pin is high
// increment mean
// value counter if
// pin is high
alm[pi] += pins[pi].value;
pi++;
if (pi == pinc)
pi = 0;
/*
if (j >= JUMPSTEPS)//if number of step is bigger than steps to skip
if (j >=
JUMPSTEPS)//if
number of step is
bigger than steps
to skip
{
//set analog pin 2 (AN0) with value from scroll
//set analog
pin 2 (AN0) with
value from scroll
//pic_set_apin(2,((5.0*(scroll1->GetPosition()))/
// (scroll1->GetRange()-1)));
//
(scroll1->GetRange()-1)));
j = -1; //reset counter
j = -1;
//reset counter
}
j++; //counter increment
j++; //counter
increment
*/
}

Expand All @@ -453,7 +491,8 @@ void cboard_Arduino_Uno::Run_CPU(void) {
if (use_spare)
SpareParts.PostProcess();

// verifiy if LEDS need update
// verifiy if LEDS need
// update
if (output_ids[O_RX]->value != pins[1].oavalue) {
output_ids[O_RX]->value = pins[1].oavalue;
output_ids[O_RX]->update = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/boards/board_Arduino_Uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class cboard_Arduino_Uno : public bsim_simavr {
// Destructor called once on board destruction
~cboard_Arduino_Uno(void);
// Called ever 100ms to draw board
void Draw(CCanvas* Canvas) override;
void Draw(void) override;
void Run_CPU(void) override;
// Return a list of board supported microcontrollers
std::string GetSupportedDevices(void) override { return "atmega328p,"; };
Expand Down
78 changes: 45 additions & 33 deletions src/boards/board_Blue_Pill.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,49 +413,51 @@ void cboard_Blue_Pill::EvMouseButtonRelease(uint button, uint x, uint y, uint st
// Called ever 100ms to draw board
// This is the critical code for simulator running speed

void cboard_Blue_Pill::Draw(CCanvas* Canvas) {
void cboard_Blue_Pill::Draw(void) {
int i;

Canvas->Init(Scale, Scale); // initialize draw context
Canvas->SetFontWeight(lxFONTWEIGHT_BOLD);
PICSimLab.CanvasCmd({CC_INIT, .Init{Scale, Scale, 0}}); // initialize draw context
PICSimLab.CanvasCmd({CC_SETFONTWEIGHT, .SetFontWeight{lxFONTWEIGHT_BOLD}});

// board_x draw
for (i = 0; i < outputc; i++) // run over all outputs
{
if (!output[i].r) // if output shape is a rectangle
{
Canvas->SetFgColor(0, 0, 0); // black
PICSimLab.CanvasCmd({CC_SETFGCOLOR, .SetFgColor{0, 0, 0}}); // black

switch (output[i].id) // search for color of output
{
case O_LED: // White using pc13 mean value
Canvas->SetColor(pins[1].oavalue, 0, 0);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{(unsigned int)pins[1].oavalue, 0, 0}});
break;
case O_LPWR: // Blue using mcupwr value
Canvas->SetColor(200 * PICSimLab.GetMcuPwr() + 55, 0, 0);
PICSimLab.CanvasCmd(
{CC_SETCOLOR, .SetColor{(unsigned int)(200 * PICSimLab.GetMcuPwr() + 55), 0, 0}});
break;
case O_RST:
Canvas->SetColor(100, 100, 100);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{100, 100, 100}});
break;
}

if (output[i].id == O_RST) {
Canvas->Circle(1, output[i].cx, output[i].cy, 15);
PICSimLab.CanvasCmd({CC_CIRCLE, .Circle{1, output[i].cx, output[i].cy, 15}});
if (p_RST) {
Canvas->SetColor(15, 15, 15);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{15, 15, 15}});
} else {
Canvas->SetColor(55, 55, 55);
PICSimLab.CanvasCmd({CC_SETCOLOR, .SetColor{55, 55, 55}});
}
Canvas->Circle(1, output[i].cx, output[i].cy, 13);
PICSimLab.CanvasCmd({CC_CIRCLE, .Circle{1, output[i].cx, output[i].cy, 13}});
} else {
Canvas->Rectangle(1, output[i].x1, output[i].y1, output[i].x2 - output[i].x1,
output[i].y2 - output[i].y1);
PICSimLab.CanvasCmd(
{CC_RECTANGLE, .Rectangle{1, output[i].x1, output[i].y1, output[i].x2 - output[i].x1,
output[i].y2 - output[i].y1}});
}
}
}

// end draw
Canvas->End();
PICSimLab.CanvasCmd({CC_END});
}

void cboard_Blue_Pill::Run_CPU_ns(uint64_t time) {
Expand All @@ -479,14 +481,18 @@ void cboard_Blue_Pill::Run_CPU_ns(uint64_t time) {
}

if (PICSimLab.GetMcuPwr()) // if powered
// for (i = 0; i < NSTEP; i++) // repeat for number of steps in 100ms
// for (i = 0; i < NSTEP;
// i++) // repeat for number
// of steps in 100ms
{
/*
if (j >= JUMPSTEPS)//if number of step is bigger than steps to skip
if (j >= JUMPSTEPS)//if number of step is bigger
than steps to skip
{
}
*/
// verify if a breakpoint is reached if not run one instruction
// verify if a breakpoint is reached if not run
// one instruction
MStep();
InstCounterInc();
// Oscilloscope window process
Expand All @@ -502,7 +508,8 @@ void cboard_Blue_Pill::Run_CPU_ns(uint64_t time) {
if (pi == pinc)
pi = 0;
/*
if (j >= JUMPSTEPS)//if number of step is bigger than steps to skip
if (j >= JUMPSTEPS)//if number of step is
bigger than steps to skip
{
j = -1; //reset counter
}
Expand Down Expand Up @@ -743,7 +750,8 @@ void cboard_Blue_Pill::MSetAPin(int pin, float value) {
if (ADCvalues[channel] != svalue) {
qemu_picsimlab_set_apin(channel, svalue);
ADCvalues[channel] = svalue;
// printf("Analog channel %02X = %i\n",channel,svalue);
// printf("Analog channel %02X =
// %i\n",channel,svalue);
}
}
}
Expand All @@ -755,7 +763,8 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) {
int port = cfg & 0x0003;
uint32_t* afio;
// int cfg_ = (cfg & 0x000C) >> 2;
// printf("Extra CFG Alternate function port(%c) pin[%02i]=0x%02X \n", port + 'A', pin, cfg_);
// printf("Extra CFG Alternate function port(%c)
// pin[%02i]=0x%02X \n", port + 'A', pin, cfg_);

switch (port) {
case 0: // GPIOA
Expand Down Expand Up @@ -1054,12 +1063,13 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) {
}
/*
// uart3
uart_afio = qemu_picsimlab_get_internals(0x1000 | STM32_UART3);
if (!(*uart_afio)) {
master_uart[2].tx_pin = iopin(B, 10);
master_uart[2].rx_pin = iopin(B, 11);
master_uart[2].ctrl_on = 1;
break
uart_afio =
qemu_picsimlab_get_internals(0x1000
| STM32_UART3); if (!(*uart_afio)) {
master_uart[2].tx_pin = iopin(B,
10); master_uart[2].rx_pin =
iopin(B, 11); master_uart[2].ctrl_on
= 1; break
}
*/
// i2c1
Expand All @@ -1080,12 +1090,13 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) {
}
/*
// uart3
uart_afio = qemu_picsimlab_get_internals(0x1000 | STM32_UART3);
if (!(*uart_afio)) {
master_uart[2].tx_pin = iopin(B, 10);
master_uart[2].rx_pin = iopin(B, 11);
master_uart[2].ctrl_on = 1;
break;
uart_afio =
qemu_picsimlab_get_internals(0x1000
| STM32_UART3); if (!(*uart_afio)) {
master_uart[2].tx_pin = iopin(B,
10); master_uart[2].rx_pin =
iopin(B, 11); master_uart[2].ctrl_on
= 1; break;
}
*/
// i2c1
Expand Down Expand Up @@ -1138,7 +1149,8 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) {
int duty = (cfg & 0xFFFF0) >> 4;
int chn = (cfg & 0x000C) >> 2;
int timer = cfg & 0x0003;
// printf("TIM %i chn %i dut set to %i\n", timer + 1, chn + 1, duty);
// printf("TIM %i chn %i dut set to %i\n", timer +
// 1, chn + 1, duty);
bitbang_pwm_set_duty(&pwm_out, (timer << 2) + chn, duty);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/boards/board_Blue_Pill.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class cboard_Blue_Pill : public bsim_qemu {
// Destructor called once on board destruction
~cboard_Blue_Pill(void);
// Called ever 100ms to draw board
void Draw(CCanvas* Canvas) override;
void Draw(void) override;
void Run_CPU(void) override{};
void Run_CPU_ns(uint64_t time) override;
// Return a list of board supported microcontrollers
Expand Down
Loading

0 comments on commit 01f111e

Please sign in to comment.