Bug fix: check if vm exists
[avr-sim.git] / Registers.h
blobf75049ebae24854085b7a8b750e5de8dd45e26fa
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 class Register {
13 public:
14 Register() {}
15 Register(word address, const std::string & name);
16 ~Register() {}
18 public:
19 void init(word address, const std::string & name);
20 unsigned char operator =(unsigned char val);
21 operator unsigned char() const;
22 void operator =(const Register & reg);
24 public:
25 void set(unsigned char val);
26 unsigned char get() const;
28 public:
29 word getAddress() const { return address; }
30 const std::string & getName() const { return name; }
32 protected:
33 word address;
34 std::string name;
35 unsigned char val;
38 class IORegister : public Register {
39 public:
40 IORegister(word address, const std::string & name,
41 unsigned char initial = 0);
43 public:
44 void reset();
45 unsigned char operator =(unsigned char val);
46 void operator |=(unsigned char val);
47 void operator &=(unsigned char val);
49 public:
50 void registerHW(Hardware *hw);
52 private:
53 unsigned char initial;
54 Hardware *hw;
57 class IORegisters {
58 public:
59 IORegisters(unsigned int count);
60 ~IORegisters();
62 public:
63 unsigned int size() const { return regs.size(); }
65 /**
66 * Reset all registers to their initial value.
68 void reset();
70 /**
71 * Reads a single bytes of raw data from the register memory
72 * at offset \e offset.
74 byte readByte(unsigned int offset) const;
76 /**
77 * Writes a single bytes of raw data to the register memory
78 * at offset \e offset.
80 void writeByte(unsigned int offset, byte val);
82 public:
83 void addReg( word addr, IORegister *reg );
84 IORegister & getIoreg(unsigned int offset) const;
85 IORegister *getIoreg(const std::string & name, bool required = true) const;
87 private:
88 std::vector<IORegister *> regs;
91 inline Register::Register(word address, const std::string & name) {
92 init( address, name );
95 inline void Register::init(word address, const std::string & name) {
96 this->address = address;
97 this->name = name;
100 inline IORegister::IORegister(word address,
101 const std::string & name, unsigned char initial)
102 : Register(address, name), initial(initial), hw(0) {
105 inline void IORegister::reset() {
106 set( initial );
109 inline void Register::set(unsigned char val) {
110 this->val = val;
113 inline unsigned char Register::get() const {
114 return val;
117 inline void Register::operator =(const Register & reg) {
118 *this = (unsigned char)reg;
121 inline byte IORegisters::readByte(unsigned int offset) const {
122 if( regs[offset] != 0 )
123 return (byte)*regs[offset];
125 return 0;
128 inline void IORegisters::writeByte(unsigned int offset, byte val) {
129 if( regs[offset] != 0 )
130 (*regs[offset]) = val;
133 inline IORegister & IORegisters::getIoreg(unsigned int offset) const {
134 return *regs[offset];
137 inline void IORegister::registerHW(Hardware *hw) {
138 this->hw = hw;
143 #endif /*AVR_REGISTERS_H*/