PICSimLab - Programmable IC Simulator Laboratory 0.9.2
PICSimLab - API
Loading...
Searching...
No Matches
bsim_simavr.h
1/* ########################################################################
2
3 PICSimLab - Programmable IC Simulator Laboratory
4
5 ########################################################################
6
7 Copyright (c) : 2010-2024 Luis Claudio GambĂ´a Lopes <lcgamboa@yahoo.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 For e-mail suggestions : lcgamboa@yahoo.com
24 ######################################################################## */
25
26#ifndef BOARD_AVR_H
27#define BOARD_AVR_H
28
29#include "../lib/board.h"
30
31#include "../devices/bitbang_uart.h"
32#include "../devices/mplabxd.h"
33#include "../lib/serial_port.h"
34
35#include <simavr/avr_adc.h>
36#include <simavr/avr_ioport.h>
37#include <simavr/avr_twi.h>
38#include <simavr/avr_uart.h>
39#include <simavr/sim_avr.h>
40#include <simavr/sim_elf.h>
41#include <simavr/sim_gdb.h>
42#include <simavr/sim_hex.h>
43
44#define MAX_UART_COUNT 4
45
46typedef struct {
47 unsigned char sdao;
48 unsigned char sclo;
49 picpin* sda;
50 picpin* scl;
51 avr_irq_t* sda_irq;
52 avr_irq_t* scl_irq;
53 unsigned char out;
54} usi_t;
55
56class bsim_simavr : virtual public board {
57public:
58 bsim_simavr(void); // Called once on board creation
59 int DebugInit(int dtyppe) override;
60 std::string GetDebugName(void) override;
61 void DebugLoop(void) override;
62 int CpuInitialized(void) override;
63 void MSetSerial(const char* port) override;
64 int MInit(const char* processor, const char* fname, float freq) override;
65 void MEnd(void) override;
66 int MGetArchitecture(void) override;
67 int MDumpMemory(const char* fname) override;
68 void MEraseFlash(void) override;
69 void MSetFreq(float freq) override;
70 float MGetFreq(void) override;
71 void MSetVCC(float vcc) override;
72 float MGetVCC(void) override;
73 float MGetInstClockFreq(void) override;
74 int MGetPinCount(void) override;
75 std::string MGetPinName(int pin) override;
76 void MSetPin(int pin, unsigned char value) override;
77 void MSetPinDOV(int pin, unsigned char ovalue) override;
78 void MSetAPin(int pin, float value) override;
79 unsigned char MGetPin(int pin) override;
80 const picpin* MGetPinsValues(void) override;
81 void MStep(void) override;
82 void MStepResume(void) override;
83 void MReset(int flags) override;
84 unsigned short* DBGGetProcID_p(void) override;
85 unsigned int DBGGetPC(void) override;
86 void DBGSetPC(unsigned int pc) override;
87 unsigned char* DBGGetRAM_p(void) override;
88 unsigned char* DBGGetROM_p(void) override;
89 unsigned char* DBGGetCONFIG_p(void) override;
90 unsigned char* DBGGetID_p(void) override;
91 unsigned char* DBGGetEEPROM_p(void) override;
92 unsigned int DBGGetRAMSize(void) override;
93 unsigned int DBGGetROMSize(void) override;
94 unsigned int DBGGetCONFIGSize(void) override;
95 unsigned int DBGGetIDSize(void) override;
96 unsigned int DBGGetEEPROM_Size(void) override;
97 void EndServers(void) override;
98 int GetDefaultClock(void) override { return 16; };
99 int GetUARTRX(const int uart_num) override;
100 int GetUARTTX(const int uart_num) override;
101 virtual void UpdateHardware(void);
102
103 static void out_hook(struct avr_irq_t* irq, uint32_t value, void* param) {
104 picpin* p = (picpin*)param;
105 p->value = value;
106 ioupdated = 1;
107 }
108
109 static void ddr_hook(struct avr_irq_t* irq, uint32_t value, void* param) {
110 picpin* p = (picpin*)param;
111 p->dir = !(value & (1 << p->pord));
112 ioupdated = 1;
113 }
114
115 void SerialSend(bitbang_uart_t* _bb_uart, const unsigned char value);
116
117 usi_t USI;
118
119protected:
120 avr_t* avr;
121 avr_irq_t* serial_irq[MAX_UART_COUNT];
122 picpin pins[256];
123 avr_irq_t* Write_stat_irq[100];
124 unsigned int serialbaud[MAX_UART_COUNT];
125 float serialexbaud[MAX_UART_COUNT];
126 void pins_reset(void);
127 int avr_debug_type;
128 serialfd_t serialfd;
129 bitbang_uart_t bb_uart[MAX_UART_COUNT];
130 unsigned char* eeprom;
131 unsigned char uart_config[MAX_UART_COUNT];
132 unsigned char usart_count;
133 unsigned int UCSR_base[MAX_UART_COUNT];
134
135private:
136 int parse_hex(const char* line, int bytes);
137 unsigned char checksum(char* str);
138 int read_ihx_avr(const char* fname, int leeprom);
139 int write_ihx_avr(const char* fname);
140
141protected:
142 int pkg;
143};
144
145#define EIMSK 0x3D
146#define UCSR0A 0XC0
147#define UCSR0B 0XC1
148#define UCSR0C 0XC2
149#define UBRR0L 0xC4
150#define UBRR0H 0xC5
151
152#define UCSR1A 0xC8
153#define UCSR1B 0XC9
154#define UBRR1L 0xCC
155#define UBRR1H 0xCD
156
157#define UCSR2A 0xD0
158#define UCSR2B 0XD1
159#define UBRR2L 0xD4
160#define UBRR2H 0xD5
161
162#define UCSR3A 0x130
163#define UCSR3B 0X131
164#define UBRR3L 0x134
165#define UBRR3H 0x135
166
167#define GIMSK 0x3B
168#define USICR 0x2D
169#define USISR 0x2E
170#define USIDR 0x2F
171#define USIBR 0x30
172
173// TinyDebug
174#define TDDR 0x3A
175#define TDCR 0x3B
176
177#endif /* BOARD_AVR_H */
Board class.
Definition board.h:111
Definition bsim_simavr.h:56
float MGetVCC(void) override
board microcontroller get vcc
Definition bsim_simavr.cc:545
int DebugInit(int dtyppe) override
Start debug support.
Definition bsim_simavr.cc:591
void MReset(int flags) override
board microcontroller reset
Definition bsim_simavr.cc:1535
unsigned int DBGGetEEPROM_Size(void) override
board microcontroller get data EEPROM memory size
Definition bsim_simavr.cc:1610
unsigned char * DBGGetCONFIG_p(void) override
board microcontroller get CONFIG memory pointer
Definition bsim_simavr.cc:1579
void MSetPin(int pin, unsigned char value) override
board microcontroller set digital pin
Definition bsim_simavr.cc:1187
float MGetFreq(void) override
board microcontroller get frequency
Definition bsim_simavr.cc:537
void MSetAPin(int pin, float value) override
board microcontroller set analog pin
Definition bsim_simavr.cc:1215
const picpin * MGetPinsValues(void) override
board microcontroller get all pins list struct
Definition bsim_simavr.cc:1388
unsigned int DBGGetRAMSize(void) override
board microcontroller get RAM memory size
Definition bsim_simavr.cc:1592
void MStep(void) override
board microcontroller run one step
Definition bsim_simavr.cc:1529
unsigned char * DBGGetID_p(void) override
board microcontroller get internal IDS memory pointer
Definition bsim_simavr.cc:1583
void EndServers(void) override
board servers shutdown
Definition bsim_simavr.cc:1614
unsigned int DBGGetIDSize(void) override
board microcontroller get internal IDS memory size
Definition bsim_simavr.cc:1605
unsigned int DBGGetROMSize(void) override
board microcontroller get ROM (FLASH) memory size
Definition bsim_simavr.cc:1596
int MGetArchitecture(void) override
Return board microcontroller architecture.
Definition bsim_simavr.cc:521
int MInit(const char *processor, const char *fname, float freq) override
board microcontroller init
Definition bsim_simavr.cc:282
int GetUARTRX(const int uart_num) override
Return the UART N RX pin number.
Definition bsim_simavr.cc:1618
unsigned char MGetPin(int pin) override
board microcontroller get digital pin value
Definition bsim_simavr.cc:1381
void MSetFreq(float freq) override
board microcontroller set frequency
Definition bsim_simavr.cc:530
int MGetPinCount(void) override
board microcontroller pin count
Definition bsim_simavr.cc:637
void MSetPinDOV(int pin, unsigned char ovalue) override
board microcontroller set Default Open Value (external pull)
Definition bsim_simavr.cc:1202
void MSetVCC(float vcc) override
board microcontroller set vcc
Definition bsim_simavr.cc:541
int GetUARTTX(const int uart_num) override
Return the UART N TX pin number.
Definition bsim_simavr.cc:1624
int CpuInitialized(void) override
return true if microcontroller is initialized
Definition bsim_simavr.cc:633
void MSetSerial(const char *port) override
Set serial port name to use.
Definition bsim_simavr.cc:82
unsigned int DBGGetCONFIGSize(void) override
board microcontroller get CONFIG memory size
Definition bsim_simavr.cc:1600
unsigned int DBGGetPC(void) override
board microcontroller get PC
Definition bsim_simavr.cc:1563
void DebugLoop(void) override
debug step (pooling)
Definition bsim_simavr.cc:622
int GetDefaultClock(void) override
Get board default clock in MHz.
Definition bsim_simavr.h:98
unsigned char * DBGGetRAM_p(void) override
board microcontroller get RAM memory pointer
Definition bsim_simavr.cc:1571
void MEraseFlash(void) override
board microcontroller erase flash memory (program)
Definition bsim_simavr.cc:525
float MGetInstClockFreq(void) override
board microcontroller get cpu internal clock (in PIC frequency/4)
Definition bsim_simavr.cc:552
unsigned char * DBGGetROM_p(void) override
board microcontroller get ROM (FLASH) memory pointer
Definition bsim_simavr.cc:1575
std::string GetDebugName(void) override
Get debug interface name.
Definition bsim_simavr.cc:614
void MStepResume(void) override
board microcontroller run one or two steps to resume instruction
Definition bsim_simavr.cc:1533
int MDumpMemory(const char *fname) override
board microcontroller save non volatile memory to hex file
Definition bsim_simavr.cc:556
unsigned char * DBGGetEEPROM_p(void) override
board microcontroller get data EEPROM memory pointer
Definition bsim_simavr.cc:1588
void MEnd(void) override
board microcontroller end
Definition bsim_simavr.cc:469
std::string MGetPinName(int pin) override
board microcontroller pin name
Definition bsim_simavr.cc:661
void DBGSetPC(unsigned int pc) override
board microcontroller set PC
Definition bsim_simavr.cc:1567
unsigned short * DBGGetProcID_p(void) override
board microcontroller get pointer to processor ID
Definition bsim_simavr.cc:1559
Definition bitbang_uart.h:35
Definition bsim_simavr.h:46