openMSX
NowindInterface.cc
Go to the documentation of this file.
1#include "NowindInterface.hh"
2#include "DiskChanger.hh"
3#include "Clock.hh"
4#include "MSXMotherBoard.hh"
5#include "MSXException.hh"
6#include "narrow.hh"
7#include "serialize.hh"
8#include "serialize_stl.hh"
9#include <cassert>
10#include <functional>
11#include <memory>
12
13namespace openmsx {
14
16 : MSXDevice(config)
17 , rom(getName() + " ROM", "rom", config)
18 , flash(rom, AmdFlashChip::AM29F040, {}, config)
19 , host(drives)
20 , basename("nowindX")
21{
22 if ((rom.size() % 0x4000) != 0) {
23 throw MSXException("NowindInterface ROM size must be a multiple of 16kB");
24 }
25 if (rom.size() == 0) {
26 throw MSXException("NowindInterface ROM size cannot be zero");
27 }
28 if (rom.size() > size_t(256 * 0x4000)) {
29 throw MSXException("NowindInterface ROM size cannot be larger than 4MB");
30 }
31
32 nowindsInUse = getMotherBoard().getSharedStuff<NowindsInUse>("nowindsInUse");
33
34 unsigned i = 0;
35 while ((*nowindsInUse)[i]) {
36 if (++i == MAX_NOWINDS) {
37 throw MSXException("Too many nowind interfaces.");
38 }
39 }
40 (*nowindsInUse)[i] = true;
41 basename[6] = char('a' + i);
42
43 command.emplace(basename, getCommandController(), *this);
44
45 // start with one (empty) drive
46 auto drive = command->createDiskChanger(basename, 0, getMotherBoard());
47 drive->createCommand();
48 drives.push_back(std::move(drive));
49
50 reset(EmuTime::dummy());
51}
52
54{
55 unsigned i = basename[6] - 'a';
56 assert((*nowindsInUse)[i]);
57 (*nowindsInUse)[i] = false;
58}
59
60void NowindInterface::reset(EmuTime::param /*time*/)
61{
62 // version 1 didn't change the bank number
63 // version 2 (produced by Sunrise) does reset the bank number
64 bank = 0;
65
66 // Flash state is NOT changed on reset
67 //flash.reset();
68}
69
70byte NowindInterface::peekMem(word address, EmuTime::param /*time*/) const
71{
72 if (((0x2000 <= address) && (address < 0x4000)) ||
73 ((0x8000 <= address) && (address < 0xA000))) {
74 return host.peek();
75 } else if ((0x4000 <= address) && (address < 0xC000)) {
76 // note: range 0x8000-0xA000 is already handled above
77 return flash.peek(bank * 0x4000 + (address & 0x3FFF));
78 } else {
79 return 0xFF;
80 }
81}
82
83byte NowindInterface::readMem(word address, EmuTime::param /*time*/)
84{
85 if (((0x2000 <= address) && (address < 0x4000)) ||
86 ((0x8000 <= address) && (address < 0xA000))) {
87 return host.read();
88 } else if ((0x4000 <= address) && (address < 0xC000)) {
89 // note: range 0x8000-0xA000 is already handled above
90 return flash.read(bank * 0x4000 + (address & 0x3FFF));
91 } else {
92 return 0xFF;
93 }
94}
95
96const byte* NowindInterface::getReadCacheLine(word address) const
97{
98 if (((0x2000 <= address) && (address < 0x4000)) ||
99 ((0x8000 <= address) && (address < 0xA000))) {
100 // nowind region, not cacheable
101 return nullptr;
102 } else if ((0x4000 <= address) && (address < 0xC000)) {
103 // note: range 0x8000-0xA000 is already handled above
104 return flash.getReadCacheLine(bank * 0x4000 + (address & 0x3FFF));
105 } else {
106 return unmappedRead.data();
107 }
108}
109
110void NowindInterface::writeMem(word address, byte value, EmuTime::param time)
111{
112 if (address < 0x4000) {
113 flash.write(bank * 0x4000 + address, value);
114 } else if (((0x4000 <= address) && (address < 0x6000)) ||
115 ((0x8000 <= address) && (address < 0xA000))) {
116 constexpr Clock<1000> clock(EmuTime::zero());
117 host.write(value, clock.getTicksTill(time));
118 } else if (((0x6000 <= address) && (address < 0x8000)) ||
119 ((0xA000 <= address) && (address < 0xC000))) {
120 auto max = narrow<uint8_t>((rom.size() / 0x4000) - 1);
121 bank = (value <= max) ? value : (value & max);
122 invalidateDeviceRCache(0x4000, 0x4000);
123 invalidateDeviceRCache(0xA000, 0x2000);
124 }
125}
126
128{
129 if (address < 0xC000) {
130 // not cacheable
131 return nullptr;
132 } else {
133 return unmappedWrite.data();
134 }
135}
136
137
138template<typename Archive>
139void NowindInterface::serialize(Archive& ar, unsigned /*version*/)
140{
141 ar.template serializeBase<MSXDevice>(*this);
142 ar.serialize("flash", flash);
143 ar.serializeWithID("drives", drives, std::ref(getMotherBoard()));
144 ar.serialize("nowindhost", host,
145 "bank", bank);
146
147 // don't serialize command, rom, basename
148}
151
152} // namespace openmsx
#define REGISTER_MSXDEVICE(CLASS, NAME)
Definition MSXDevice.hh:356
void write(size_t address, uint8_t value)
Definition AmdFlash.cc:456
const uint8_t * getReadCacheLine(size_t address) const
Definition AmdFlash.cc:444
uint8_t read(size_t address)
Definition AmdFlash.cc:432
uint8_t peek(size_t address) const
Definition AmdFlash.cc:186
Represents a clock with a fixed frequency.
Definition Clock.hh:19
constexpr unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
Definition Clock.hh:58
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition MSXDevice.cc:70
void invalidateDeviceRCache()
Definition MSXDevice.hh:215
static std::array< byte, 0x10000 > unmappedRead
Definition MSXDevice.hh:306
static std::array< byte, 0x10000 > unmappedWrite
Definition MSXDevice.hh:307
byte peek() const
Definition NowindHost.cc:52
void write(byte data, unsigned time)
Definition NowindHost.cc:70
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
byte * getWriteCacheLine(word address) override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
void reset(EmuTime::param time) override
This method is called on reset.
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
NowindInterface(const DeviceConfig &config)
void serialize(Archive &ar, unsigned version)
void writeMem(word address, byte value, EmuTime::param time) override
Write a given byte to a given location at a certain time to this device.
auto size() const
Definition Rom.hh:36
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)