avr: Construct the pin table more carefully
[avr-sim.git] / src / Registers.h
blobe4d1c2fcf5456ea4f2d662153e59692fb3a65295
1 #ifndef AVR_REGISTERS_H
2 #define AVR_REGISTERS_H
4 #include <vector>
5 #include <string>
6 #include "Memory.h"
8 namespace avr {
10 class Hardware;
12 /**
13 * @author Tom Haber
14 * @date Apr 23, 2008
15 * @brief A register
17 * Represents general purpose registers and IORegisters
19 class Register {
20 public:
21 Register() {}
22 Register(word address, const std::string & name);
23 ~Register() {}
25 public:
26 /**
27 * Initialize the register
28 * @param address the address of the register (IO address not mem)
29 * @param name the name of the register
31 void init(word address, const std::string & name);
33 /**
34 * Changes the register value to \e val.
36 unsigned char operator =(unsigned char val);
38 /**
39 * Returns the register value.
41 operator unsigned char() const;
43 /**
44 * Changes the register value to the value of register \reg.
46 void operator =(const Register & reg);
48 public:
49 void set(unsigned char val);
50 unsigned char get() const;
52 public:
53 /**
54 * Returns the IO address of the register
56 word getAddress() const { return address; }
58 /**
59 * Returns the name of the register
61 const std::string & getName() const { return name; }
63 protected:
64 word address;
65 std::string name;
66 unsigned char val;
69 /**
70 * @author Tom Haber
71 * @date Apr 23, 2008
72 * @brief An IOregister
74 * Adds functionality to Register for IORegisters.
75 * Hardware can be bound to an IORegister and it will be
76 * notified on changes.
78 * Some registers share an I/O location with another register.
79 * When doing a write access to this I/O location, the high bit
80 * controls which one of the two registers will be written.
82 * Read access is more complex: the read access is controlled by a
83 * timed sequence. Reading the I/O location once returns the first
84 * register content. If the register location was read in the
85 * previous clock cycle, reading the register in the current cycle
86 * will return the second register content.
88 * Note that the timed sequence for reading the shared location is
89 * an atomic operation. Interrupts must therefore be controlled
90 * (e.g., by disabling interrupts globally) during the read operation.
92 * These type of locations have to be taken into account elsewhere!
94 class IORegister : public Register {
95 public:
96 IORegister(word address, const std::string & name,
97 unsigned char initial = 0);
99 public:
101 * Reset the register to its initial value.
103 void reset();
106 * Returns the register value.
108 operator unsigned char();
111 * Changes the register value to \e val
113 unsigned char operator =(unsigned char val);
115 void operator |=(unsigned char val);
116 void operator &=(unsigned char val);
118 public:
120 * Bind an internal device to this register.
121 * The hardware will be notified on register value changes.
123 void registerHW(Hardware *hw);
125 private:
126 unsigned char initial;
127 Hardware *hw;
131 * @author Tom Haber
132 * @date Apr 23, 2008
133 * @brief All IORegisters
135 * Contains all IORegisters and provides an interface for the MMU.
137 class IORegisters {
138 public:
139 IORegisters(unsigned int count);
140 ~IORegisters();
142 public:
143 unsigned int size() const { return regs.size(); }
146 * Reset all registers to their initial value.
148 void reset();
151 * Reads a single bytes of raw data from the register memory
152 * at offset \e offset.
154 byte readByte(unsigned int offset) const;
157 * Writes a single bytes of raw data to the register memory
158 * at offset \e offset.
160 void writeByte(unsigned int offset, byte val);
162 public:
163 void addReg( word addr, IORegister *reg );
164 IORegister & getIoreg(unsigned int offset) const;
165 IORegister *getIoreg(const std::string & name, bool required = true) const;
167 private:
168 std::vector<IORegister *> regs;
171 inline Register::Register(word address, const std::string & name) {
172 init( address, name );
175 inline void Register::init(word address, const std::string & name) {
176 this->address = address;
177 this->name = name;
180 inline IORegister::IORegister(word address,
181 const std::string & name, unsigned char initial)
182 : Register(address, name), initial(initial), hw(0) {
185 inline void IORegister::reset() {
186 set( initial );
189 inline void Register::set(unsigned char val) {
190 this->val = val;
193 inline unsigned char Register::get() const {
194 return val;
197 inline void Register::operator =(const Register & reg) {
198 *this = (unsigned char)reg;
201 inline byte IORegisters::readByte(unsigned int offset) const {
202 if( regs[offset] != 0 )
203 return (byte)*regs[offset];
205 return 0;
208 inline unsigned char Register::operator =(unsigned char val) {
209 unsigned char ret = this->val;
210 this->val = val;
211 return ret;
214 inline Register::operator unsigned char() const {
215 return val;
218 inline void IORegisters::writeByte(unsigned int offset, byte val) {
219 if( regs[offset] != 0 )
220 (*regs[offset]) = val;
223 inline IORegister & IORegisters::getIoreg(unsigned int offset) const {
224 return *regs[offset];
227 inline void IORegister::registerHW(Hardware *hw) {
228 this->hw = hw;
233 #endif /*AVR_REGISTERS_H*/