next up previous contents
Next: About this document ... Up: Builtin I/O Previous: Builtin I/O

Example - Output to the console

# comment

An example class is included into the project. This class handles output to the console by printing out everything written to address 49152. The declaration can be used for further extensions:

#include "Memory.hpp"

namespace Interpreter {
  
  class IOTtyOut : public Memory
  {
  public:
    // Con and Destructor:
    IOTtyOut(const unsigned int start = 0);
    IOTtyOut(const IOTtyOut &);
    virtual ~IOTtyOut();

    // The read and write functions reimplemented for IOTtyOut
    virtual bool write(const unsigned int addr, const short val);
    virtual bool read(const unsigned int addr, short &) const;
  };
}

The implementation is very simple:

#include "IOTtyOut.hpp"

namespace Interpreter {

  // IOTtyOut::IOTtyOut(int)
  // Initializes the IOTtyOut. The content of the IOTtyOut is undefined.
  IOTtyOut::IOTtyOut(const unsigned int start)
    : Memory(1, start)
  { }

  // IOTtyOut::IOTtyOut(const IOTtyOut &)
  // Copies the memory. The content is copied 1:1. If there was no memory
  // in the argument new memory will be created.
  IOTtyOut::IOTtyOut(const IOTtyOut &m)
    : Memory(1, m.start)
  { }

  // IOTtyOut::~IOTtyOut()
  // Frees the IOTtyOut.
  IOTtyOut::~IOTtyOut()
  { }

  // bool IOTtyOut::write(const unsigned int, const short)
  // Writes a ASCII char to a tty. This is only done if the address is equal
  // to start. True or false will be returned according to the success of
  // the operation.
  bool IOTtyOut::write(const unsigned int addr, const short val)
  {
    if (addr == start) {
      std::cout << static_cast<char>((val & 0xff));
      return true;
    }
    return false;
  }

  // bool IOTtyOut::read(const unsigned int, short &)
  // False will be returned and the value is set to -1 (0xffff).
  bool IOTtyOut::read(const unsigned int addr, short &val) const
  {
    val = -1; // don't produce warnings on unused vars...
    return false;
  }
}

Such a simple class is really easy to implement.



Markus Hennecke
9/14/2003