Skip to content

Commit 487ee2f

Browse files
ChetSimpsonejaquay
authored andcommitted
update cartridge loader to support loading a cartridge implementing the cartridge interface
* Adds `cartridge_context` to provide a fluent interface to the host environment. * Adds cartridge factory definitions for the functions exported by the dll to create a `cartridge`. * Updates `cartridge_loader` to search for `GetPakFactory` in the loaded dll and if present use it to construct a `cartridge`. * Updates `rom_cartridge` to use the `cartridge_context` interface instead of callbacks. * Adds basic support for loading a ROM image (separate from loading a ROM cartridge/program pak). * Updates MPI implementation to provide addition implementations of `cartridge_context` for use in mounting a cartridge.
1 parent 0a7cfc4 commit 487ee2f

20 files changed

+448
-40
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Copyright 2015 by Joseph Forgione
3+
// This file is part of VCC (Virtual Color Computer).
4+
//
5+
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
6+
// modify it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or (at your
8+
// option) any later version.
9+
//
10+
// VCC (Virtual Color Computer) is distributed in the hope that it will be
11+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13+
// Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License along with
16+
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
17+
////////////////////////////////////////////////////////////////////////////////
18+
#pragma once
19+
#include <vcc/core/detail/exports.h>
20+
#include <vcc/core/interrupts.h>
21+
#include <string>
22+
23+
24+
// FIXME: this needs to come from the common library but is currently part of the
25+
// main vcc app. Update this when it is migrated.
26+
enum MenuItemType;
27+
28+
namespace vcc { namespace core
29+
{
30+
31+
struct LIBCOMMON_EXPORT cartridge_context
32+
{
33+
using path_type = ::std::string;
34+
35+
36+
virtual ~cartridge_context() = default;
37+
38+
virtual path_type configuration_path() const = 0;
39+
40+
virtual void reset() = 0;
41+
42+
virtual void assert_cartridge_line(bool line_state) = 0;
43+
virtual void assert_interrupt(Interrupt interrupt, InterruptSource interrupt_source) = 0;
44+
45+
virtual void write_memory_byte(unsigned char value, unsigned short address) = 0;
46+
virtual unsigned char read_memory_byte(unsigned short address) = 0;
47+
48+
virtual void add_menu_item(const char* menu_name, int menu_id, MenuItemType menu_type) = 0;
49+
};
50+
51+
} }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Copyright 2015 by Joseph Forgione
3+
// This file is part of VCC (Virtual Color Computer).
4+
//
5+
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
6+
// modify it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or (at your
8+
// option) any later version.
9+
//
10+
// VCC (Virtual Color Computer) is distributed in the hope that it will be
11+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13+
// Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License along with
16+
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
17+
////////////////////////////////////////////////////////////////////////////////
18+
#pragma once
19+
#include <vcc/core/cartridge.h>
20+
#include <vcc/core/cartridge_context.h>
21+
#include <memory>
22+
23+
using CreatePakFactoryFunction = std::unique_ptr<::vcc::core::cartridge> (*)(std::unique_ptr<::vcc::core::cartridge_context> context);
24+
using GetPakFactoryFunction = CreatePakFactoryFunction(*)();
25+

libcommon/include/vcc/core/cartridge_loader.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
////////////////////////////////////////////////////////////////////////////////
1818
#pragma once
1919
#include <vcc/core/cartridge.h>
20+
#include <vcc/core/cartridge_context.h>
2021
#include <vcc/core/legacy_cartridge_definitions.h>
2122
#include <vcc/core/utils/dll_deleter.h>
2223
#include <string>
@@ -58,7 +59,7 @@ namespace vcc { namespace core
5859
cartridge_loader_status load_result = cartridge_loader_status::not_loaded;
5960
};
6061

61-
struct LIBCOMMON_EXPORT cartridge_loader_context
62+
struct cartridge_loader_context
6263
{
6364
const AppendCartridgeMenuModuleCallback addMenuItemCallback;
6465
const ReadMemoryByteModuleCallback readData;
@@ -69,13 +70,15 @@ namespace vcc { namespace core
6970

7071
LIBCOMMON_EXPORT cartridge_file_type determine_cartridge_type(const std::string& filename);
7172
LIBCOMMON_EXPORT cartridge_loader_result load_rom_cartridge(
72-
const cartridge_loader_context& context,
73+
std::unique_ptr<cartridge_context> context,
7374
const std::string& filename);
7475
LIBCOMMON_EXPORT cartridge_loader_result load_legacy_cartridge(
76+
std::unique_ptr<cartridge_context> cartridge_context,
7577
const cartridge_loader_context& context,
7678
const std::string& filename,
7779
const std::string& iniPath);
7880
LIBCOMMON_EXPORT cartridge_loader_result load_cartridge(
81+
std::unique_ptr<cartridge_context> cartridge_context,
7982
const cartridge_loader_context& context,
8083
const std::string& filename,
8184
const std::string& iniPath);

libcommon/include/vcc/core/cartridges/rom_cartridge.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
////////////////////////////////////////////////////////////////////////////////
1818
#pragma once
1919
#include <vcc/core/cartridges/basic_cartridge.h>
20-
#include <vcc/core/legacy_cartridge_definitions.h>
20+
#include <vcc/core/cartridge_context.h>
2121
#include <vector>
22+
#include <memory>
2223

2324

2425
namespace vcc { namespace core { namespace cartridges
@@ -28,6 +29,7 @@ namespace vcc { namespace core { namespace cartridges
2829
{
2930
public:
3031

32+
using context_type = ::vcc::core::cartridge_context;
3133
using buffer_type = std::vector<uint8_t>;
3234
using size_type = std::size_t;
3335

@@ -38,7 +40,7 @@ namespace vcc { namespace core { namespace cartridges
3840

3941

4042
LIBCOMMON_EXPORT rom_cartridge(
41-
AssertCartridgeLineModuleCallback assertCartCallback,
43+
std::unique_ptr<context_type> context,
4244
name_type name,
4345
catalog_id_type catalog_id,
4446
buffer_type buffer,
@@ -59,7 +61,7 @@ namespace vcc { namespace core { namespace cartridges
5961

6062
private:
6163

62-
const AssertCartridgeLineModuleCallback assertCartCallback_;
64+
const std::unique_ptr<context_type> context_;
6365
const name_type name_;
6466
const catalog_id_type catalog_id_;
6567
const buffer_type buffer_;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Copyright 2015 by Joseph Forgione
3+
// This file is part of VCC (Virtual Color Computer).
4+
//
5+
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
6+
// modify it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or (at your
8+
// option) any later version.
9+
//
10+
// VCC (Virtual Color Computer) is distributed in the hope that it will be
11+
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13+
// Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License along with
16+
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
17+
////////////////////////////////////////////////////////////////////////////////
18+
#pragma once
19+
#include <vcc/core/detail/exports.h>
20+
#include <string>
21+
#include <vector>
22+
23+
namespace vcc { namespace core
24+
{
25+
26+
class rom_image
27+
{
28+
public:
29+
30+
using path_type = std::string;
31+
using container_type = std::vector<unsigned char>;
32+
using value_type = container_type::value_type;
33+
using size_type = container_type::size_type;
34+
35+
36+
public:
37+
38+
LIBCOMMON_EXPORT rom_image() = default;
39+
LIBCOMMON_EXPORT ~rom_image() = default;
40+
41+
bool empty() const
42+
{
43+
return data_.empty();
44+
}
45+
46+
path_type filename() const
47+
{
48+
return filename_;
49+
}
50+
51+
LIBCOMMON_EXPORT bool load(path_type filename);
52+
53+
void clear()
54+
{
55+
filename_.clear();
56+
data_.clear();
57+
}
58+
59+
value_type read_memory_byte(size_type memory_address) const
60+
{
61+
if (data_.empty())
62+
{
63+
return 0;
64+
}
65+
66+
return data_[memory_address % data_.size()];
67+
}
68+
69+
70+
private:
71+
72+
path_type filename_;
73+
container_type data_;
74+
};
75+
76+
} }

libcommon/libcommon.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<ClCompile Include="src\core\cartridges\legacy_cartridge.cpp" />
103103
<ClCompile Include="src\core\cartridges\rom_cartridge.cpp" />
104104
<ClCompile Include="src\core\cartridge_loader.cpp" />
105+
<ClCompile Include="src\core\rom_image.cpp" />
105106
<ClCompile Include="src\core\utils\configuration_serializer.cpp" />
106107
<ClCompile Include="src\core\utils\filesystem.cpp" />
107108
<ClCompile Include="src\core\utils\winapi.cpp" />
@@ -119,11 +120,14 @@
119120
<ClInclude Include="include\vcc\core\cartridges\legacy_cartridge.h" />
120121
<ClInclude Include="include\vcc\core\cartridges\null_cartridge.h" />
121122
<ClInclude Include="include\vcc\core\cartridges\rom_cartridge.h" />
123+
<ClInclude Include="include\vcc\core\cartridge_context.h" />
124+
<ClInclude Include="include\vcc\core\cartridge_factory.h" />
122125
<ClInclude Include="include\vcc\core\cartridge_loader.h" />
123126
<ClInclude Include="include\vcc\core\detail\exports.h" />
124127
<ClInclude Include="include\vcc\core\interrupts.h" />
125128
<ClInclude Include="include\vcc\core\legacy_cartridge_definitions.h" />
126129
<ClInclude Include="include\vcc\core\limits.h" />
130+
<ClInclude Include="include\vcc\core\rom_image.h" />
127131
<ClInclude Include="include\vcc\core\utils\configuration_serializer.h" />
128132
<ClInclude Include="include\vcc\core\utils\critical_section.h" />
129133
<ClInclude Include="include\vcc\core\utils\dll_deleter.h" />

libcommon/libcommon.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<ClCompile Include="src\core\cartridges\basic_cartridge.cpp">
7676
<Filter>Source Files\core\cartridges</Filter>
7777
</ClCompile>
78+
<ClCompile Include="src\core\rom_image.cpp">
79+
<Filter>Source Files\core</Filter>
80+
</ClCompile>
7881
</ItemGroup>
7982
<ItemGroup>
8083
<ClInclude Include="resource\resource.h">
@@ -134,6 +137,15 @@
134137
<ClInclude Include="include\vcc\core\cartridges\basic_cartridge.h">
135138
<Filter>Header Files\core\cartridges</Filter>
136139
</ClInclude>
140+
<ClInclude Include="include\vcc\core\cartridge_context.h">
141+
<Filter>Header Files\core</Filter>
142+
</ClInclude>
143+
<ClInclude Include="include\vcc\core\cartridge_factory.h">
144+
<Filter>Header Files\core</Filter>
145+
</ClInclude>
146+
<ClInclude Include="include\vcc\core\rom_image.h">
147+
<Filter>Header Files\core</Filter>
148+
</ClInclude>
137149
</ItemGroup>
138150
<ItemGroup>
139151
<ResourceCompile Include="resource\libcommon.rc">

libcommon/src/core/cartridge_loader.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <vector>
2222
#include <fstream>
2323
#include <iterator>
24+
#include <vcc/core/cartridge_factory.h>
2425

2526

2627
namespace vcc { namespace core
@@ -61,7 +62,7 @@ namespace vcc { namespace core
6162
}
6263

6364
cartridge_loader_result load_rom_cartridge(
64-
const cartridge_loader_context& context,
65+
std::unique_ptr<cartridge_context> context,
6566
const std::string& filename)
6667
{
6768
constexpr size_t PAK_MAX_MEM = 0x40000;
@@ -96,7 +97,7 @@ namespace vcc { namespace core
9697
return {
9798
nullptr,
9899
std::make_unique<vcc::core::cartridges::rom_cartridge>(
99-
context.assertCartridgeLine,
100+
move(context),
100101
extract_filename(filename),
101102
"",
102103
move(romImage),
@@ -106,6 +107,7 @@ namespace vcc { namespace core
106107
}
107108

108109
cartridge_loader_result load_legacy_cartridge(
110+
std::unique_ptr<cartridge_context> cartridge_context,
109111
const cartridge_loader_context& context,
110112
const std::string& filename,
111113
const std::string& iniPath)
@@ -123,25 +125,37 @@ namespace vcc { namespace core
123125
return { nullptr, nullptr, cartridge_loader_status::cannot_open };
124126
}
125127

126-
if (GetProcAddress(details.handle.get(), "ModuleName") == nullptr)
128+
const auto factoryAccessor(reinterpret_cast<GetPakFactoryFunction>(GetProcAddress(
129+
details.handle.get(),
130+
"GetPakFactory")));
131+
if (factoryAccessor != nullptr)
127132
{
128-
return { nullptr, nullptr, cartridge_loader_status::not_expansion };
133+
details.cartridge = factoryAccessor()(move(cartridge_context));
134+
details.load_result = cartridge_loader_status::success;
135+
136+
return details;
129137
}
130138

131-
details.cartridge = std::make_unique<vcc::core::cartridges::legacy_cartridge>(
132-
details.handle.get(),
133-
iniPath,
134-
context.addMenuItemCallback,
135-
context.readData,
136-
context.writeData,
137-
context.assertInterrupt,
138-
context.assertCartridgeLine);
139-
details.load_result = cartridge_loader_status::success;
140-
141-
return details;
139+
if (GetProcAddress(details.handle.get(), "ModuleName") != nullptr)
140+
{
141+
details.cartridge = std::make_unique<vcc::core::cartridges::legacy_cartridge>(
142+
details.handle.get(),
143+
iniPath,
144+
context.addMenuItemCallback,
145+
context.readData,
146+
context.writeData,
147+
context.assertInterrupt,
148+
context.assertCartridgeLine);
149+
details.load_result = cartridge_loader_status::success;
150+
151+
return details;
152+
}
153+
154+
return { nullptr, nullptr, cartridge_loader_status::not_expansion };
142155
}
143156

144157
cartridge_loader_result load_cartridge(
158+
std::unique_ptr<cartridge_context> cartridge_context,
145159
const cartridge_loader_context& context,
146160
const std::string& filename,
147161
const std::string& iniPath)
@@ -153,10 +167,10 @@ namespace vcc { namespace core
153167
return { nullptr, nullptr, cartridge_loader_status::cannot_open };
154168

155169
case cartridge_file_type::rom_image: // File is a ROM image
156-
return vcc::core::load_rom_cartridge(context, filename);
170+
return vcc::core::load_rom_cartridge(move(cartridge_context), filename);
157171

158172
case cartridge_file_type::library: // File is a DLL
159-
return vcc::core::load_legacy_cartridge(context, filename, iniPath);
173+
return vcc::core::load_legacy_cartridge(move(cartridge_context), context, filename, iniPath);
160174
}
161175
}
162176

libcommon/src/core/cartridges/rom_cartridge.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ namespace vcc { namespace core { namespace cartridges
2222
{
2323

2424
rom_cartridge::rom_cartridge(
25-
AssertCartridgeLineModuleCallback assertCartCallback,
25+
std::unique_ptr<context_type> context,
2626
name_type name,
2727
catalog_id_type catalog_id,
2828
buffer_type buffer,
2929
bool enable_bank_switching)
3030
:
31-
assertCartCallback_(assertCartCallback),
31+
context_(move(context)),
3232
name_(move(name)),
3333
catalog_id_(move(catalog_id)),
3434
buffer_(move(buffer)),
@@ -68,7 +68,7 @@ namespace vcc { namespace core { namespace cartridges
6868

6969
void rom_cartridge::initialize_bus()
7070
{
71-
assertCartCallback_(true);
71+
context_->assert_cartridge_line(true);
7272
}
7373

7474
} } }

0 commit comments

Comments
 (0)