-
Notifications
You must be signed in to change notification settings - Fork 1
Add support for custom peripherals #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+179
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1b7d90
Add mock CharMon (character-based monitor) peripheral.
giomba 2b0d56b
Add "User Bus" module.
giomba 3e1a3a0
Add support for custom peripherals bus (UserBus)
giomba 5acad2c
Add example configuration to enable the CharMon device.
giomba 3206f2c
Reverse logic for bus peripherals overlapping check.
giomba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #include "charmon.h" | ||
|
|
||
| #include "conf.h" | ||
| #include "ubus.h" | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #define CHARMON_BASE (0xF0) | ||
|
|
||
| void charmon_out(ceda_ioaddr_t address, uint8_t value) { | ||
| (void)address; | ||
| (void)putc(value, stdout); | ||
| } | ||
|
|
||
| void charmon_init(CEDAModule *mod) { | ||
| memset(mod, 0, sizeof(*mod)); | ||
|
|
||
| bool *conf_installed = conf_getBool("mod", "charmon_installed"); | ||
| bool installed = conf_installed ? *conf_installed : false; | ||
|
|
||
| if (!installed) | ||
| return; | ||
|
|
||
| ubus_register(CHARMON_BASE, CHARMON_BASE + 1, NULL, charmon_out); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #ifndef CEDA_CHAR_MONITOR_H | ||
| #define CEDA_CHAR_MONITOR_H | ||
|
|
||
| #include "module.h" | ||
| #include "type.h" | ||
|
|
||
| void charmon_init(CEDAModule *mod); | ||
| void charmon_out(ceda_ioaddr_t address, uint8_t value); | ||
|
|
||
| #endif // CEDA_CHAR_MONITOR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #include "ubus.h" | ||
|
|
||
| #include "conf.h" | ||
|
|
||
| #include <stddef.h> | ||
| #include <string.h> | ||
|
|
||
| #define UBUS_MAX_PERIPHERALS (4) | ||
|
|
||
| #define LOG_LEVEL LOG_LVL_INFO | ||
| #include "log.h" | ||
|
|
||
| struct ubus_io_slot { | ||
| ceda_ioaddr_t base; | ||
| uint32_t top; | ||
| ubus_io_read_t in; | ||
| ubus_io_write_t out; | ||
| }; | ||
|
|
||
| static struct ubus_io_slot ubus_slots[UBUS_MAX_PERIPHERALS]; | ||
| static size_t ubus_used = 0; | ||
|
|
||
| void ubus_init(CEDAModule *mod) { | ||
| memset(mod, 0, sizeof(*mod)); | ||
|
|
||
| mod->init = ubus_init; | ||
| } | ||
|
|
||
| bool ubus_register(ceda_ioaddr_t base, uint32_t top, ubus_io_read_t read, | ||
| ubus_io_write_t write) { | ||
| if (!read && !write) | ||
| return false; | ||
|
|
||
| if (top > 0x100) | ||
| return false; | ||
|
|
||
| if (top <= base) | ||
| return false; | ||
|
|
||
| if (ubus_used == UBUS_MAX_PERIPHERALS) { | ||
| LOG_WARN("Too many peripherals registered\n"); | ||
| return false; | ||
| } | ||
|
|
||
| // check if peripherals are overlapping | ||
| for (size_t i = 0; i < ubus_used; ++i) { | ||
| struct ubus_io_slot *slot = &ubus_slots[i]; | ||
| bool ok = (top <= slot->base) || (base >= slot->top); | ||
| if (!ok) | ||
| return false; | ||
| } | ||
|
|
||
| ubus_slots[ubus_used].base = base; | ||
| ubus_slots[ubus_used].top = top; | ||
| ubus_slots[ubus_used].in = read; | ||
| ubus_slots[ubus_used].out = write; | ||
| ubus_used += 1; | ||
|
|
||
| LOG_INFO("Registered peripheral at %02x\n", (unsigned int)base); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| zuint8 ubus_io_in(ceda_ioaddr_t address) { | ||
| for (size_t i = 0; i < ubus_used; ++i) { | ||
| struct ubus_io_slot *slot = &ubus_slots[i]; | ||
| if (address >= slot->base && address < slot->top) | ||
| if (slot->in) | ||
| return slot->in(address - slot->base); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void ubus_io_out(ceda_ioaddr_t address, uint8_t value) { | ||
| for (size_t i = 0; i < ubus_used; ++i) { | ||
| struct ubus_io_slot *slot = &ubus_slots[i]; | ||
| if (address >= slot->base && address < slot->top) | ||
| if (slot->out) { | ||
| slot->out(address - slot->base, value); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef CEDA_USER_BUS_H | ||
| #define CEDA_USER_BUS_H | ||
|
|
||
| #include "module.h" | ||
| #include "type.h" | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| #include <Z80.h> | ||
|
|
||
| typedef uint8_t (*ubus_io_read_t)(ceda_ioaddr_t address); | ||
| typedef void (*ubus_io_write_t)(ceda_ioaddr_t address, uint8_t value); | ||
|
|
||
| /** | ||
| * @brief Initialize the User Bus module. | ||
| * | ||
| * This module allows users to connect custom peripherals to the computer bus. | ||
| */ | ||
| void ubus_init(CEDAModule *mod); | ||
|
|
||
| /** | ||
| * @brief Register a peripheral on the bus. | ||
| * | ||
| * @param base Peripheral base address. | ||
| * @param top Peripheral top address + 1 (eg. the first unused address) | ||
| * @param read IO input callback. | ||
| * @param write IO output callback. | ||
| * | ||
| * @return true in case of success, false otherwise. | ||
| */ | ||
| bool ubus_register(ceda_ioaddr_t base, uint32_t top, ubus_io_read_t read, | ||
| ubus_io_write_t write); | ||
|
|
||
| zuint8 ubus_io_in(ceda_ioaddr_t address); | ||
| void ubus_io_out(ceda_ioaddr_t address, uint8_t value); | ||
|
|
||
| #endif // CEDA_USER_BUS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.