Merge remote-tracking branch 'origin/machine-yosemite'

This commit is contained in:
Maxim Poliakovski 2023-04-21 12:49:58 +02:00
commit 03595c3940
27 changed files with 1142 additions and 266 deletions

View File

@ -94,7 +94,7 @@ enum SPR : int {
DAR = 19,
RTCU_S = 20, // supervisor RTCU
RTCL_S = 21, // supervisor RTCL
DEC = 22,
DEC = 22, // decrementer
SDR1 = 25,
SRR0 = 26,
SRR1 = 27,
@ -160,8 +160,10 @@ extern uint32_t opcode_value; // used for interpreting opcodes
extern uint64_t timebase_counter;
extern uint64_t tbr_wr_timestamp;
extern uint64_t dec_wr_timestamp;
extern uint64_t rtc_timestamp;
extern uint64_t tbr_wr_value;
extern uint32_t dec_wr_value;
extern uint32_t tbr_freq_ghz;
extern uint32_t rtc_lo, rtc_hi;

View File

@ -71,7 +71,8 @@ uint64_t rtc_timestamp; // stores vCPU virtual time of the last RTC write
uint64_t tbr_wr_value; // last value written to the TBR
uint32_t tbr_freq_ghz; // TBR/RTC driving frequency in GHz expressed as a 32 bit fraction less than 1.0.
uint64_t timebase_counter; // internal timebase counter
uint32_t decr; // current value of PPC DEC register
uint64_t dec_wr_timestamp; // stores vCPU virtual time of the last DEC write
uint32_t dec_wr_value; // last value written to the DEC register
uint32_t rtc_lo; // MPC601 RTC lower, counts nanoseconds
uint32_t rtc_hi; // MPC601 RTC upper, counts seconds
@ -771,7 +772,7 @@ void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version, uint64_t tb_freq)
exec_flags = 0;
timebase_counter = 0;
decr = 0;
dec_wr_value = 0;
/* zero all GPRs as prescribed for MPC601 */
/* For later PPC CPUs, GPR content is undefined */

View File

@ -870,6 +870,35 @@ static inline void calc_rtcl_value()
rtc_timestamp = new_ts;
}
static inline uint64_t calc_tbr_value()
{
uint64_t tbr_inc;
uint32_t tbr_inc_lo;
uint64_t diff = get_virt_time_ns() - tbr_wr_timestamp;
_u32xu64(tbr_freq_ghz, diff, tbr_inc, tbr_inc_lo);
return (tbr_wr_value + tbr_inc);
}
static inline uint32_t calc_dec_value() {
uint64_t dec_adj;
uint32_t dec_adj_lo;
uint64_t diff = get_virt_time_ns() - dec_wr_timestamp;
_u32xu64(tbr_freq_ghz, diff, dec_adj, dec_adj_lo);
return (dec_wr_value - dec_adj);
}
static void update_timebase(uint64_t mask, uint64_t new_val)
{
uint64_t tbr_value = calc_tbr_value();
tbr_wr_value = (tbr_value & mask) | new_val;
tbr_wr_timestamp = get_virt_time_ns();
}
static void update_decrementer(uint32_t val) {
dec_wr_value = val;
dec_wr_timestamp = get_virt_time_ns();
}
void dppc_interpreter::ppc_mfspr() {
uint32_t ref_spr = (((ppc_cur_instruction >> 11) & 31) << 5) | ((ppc_cur_instruction >> 16) & 31);
@ -888,25 +917,14 @@ void dppc_interpreter::ppc_mfspr() {
calc_rtcl_value();
ppc_state.spr[SPR::RTCU_U] = rtc_hi;
break;
case SPR::DEC:
ppc_state.spr[SPR::DEC] = calc_dec_value();
break;
}
ppc_state.gpr[(ppc_cur_instruction >> 21) & 31] = ppc_state.spr[ref_spr];
}
static inline uint64_t calc_tbr_value()
{
uint64_t diff = get_virt_time_ns() - tbr_wr_timestamp;
uint64_t tbr_inc; uint32_t tbr_inc_lo; _u32xu64(tbr_freq_ghz, diff, tbr_inc, tbr_inc_lo);
return (tbr_wr_value + tbr_inc);
}
static void update_timebase(uint64_t mask, uint64_t new_val)
{
uint64_t tbr_value = calc_tbr_value();
tbr_wr_value = (tbr_value & mask) | new_val;
tbr_wr_timestamp = get_virt_time_ns();
}
void dppc_interpreter::ppc_mtspr() {
uint32_t ref_spr = (((ppc_cur_instruction >> 11) & 31) << 5) | ((ppc_cur_instruction >> 16) & 31);
reg_s = (ppc_cur_instruction >> 21) & 31;
@ -936,6 +954,9 @@ void dppc_interpreter::ppc_mtspr() {
calc_rtcl_value();
rtc_hi = val;
break;
case SPR::DEC:
update_decrementer(val);
break;
case SPR::TBL_S:
update_timebase(0xFFFFFFFF00000000ULL, val);
break;

View File

@ -410,7 +410,7 @@ void enter_debugger() {
int log_level, context;
size_t separator_pos;
unique_ptr<OfNvramUtils> ofnvram = unique_ptr<OfNvramUtils>(new OfNvramUtils);
unique_ptr<OfConfigUtils> ofnvram = unique_ptr<OfConfigUtils>(new OfConfigUtils);
context = 1; /* start with the PowerPC context */

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -44,77 +44,100 @@ void DMAChannel::fetch_cmd(uint32_t cmd_addr, DMACmd* p_cmd) {
uint8_t DMAChannel::interpret_cmd() {
DMACmd cmd_struct;
bool is_writable;
int cmd;
bool is_writable, branch_taken = false;
if (this->cmd_in_progress) {
// return current command if there is data to transfer
if (this->queue_len)
return cmd;
return this->cur_cmd;
// obtain real pointer to the descriptor of the completed command
uint8_t *curr_cmd = mmu_get_dma_mem(this->cmd_ptr - 16, 16, &is_writable);
uint8_t *cmd_desc = mmu_get_dma_mem(this->cmd_ptr, 16, &is_writable);
// get command code
cmd = curr_cmd[3] >> 4;
this->cur_cmd = cmd_desc[3] >> 4;
// all commands except STOP update cmd.xferStatus
if (cmd < 7 && is_writable) {
WRITE_WORD_LE_A(&curr_cmd[14], this->ch_stat | CH_STAT_ACTIVE);
// all commands except STOP update cmd.xferStatus and
// perform actions under control of "i", "b" and "w" bits
if (this->cur_cmd < DBDMA_Cmd::STOP) {
if (is_writable)
WRITE_WORD_LE_A(&cmd_desc[14], this->ch_stat | CH_STAT_ACTIVE);
if (cmd_desc[2] & 3) {
ABORT_F("DBDMA: cmd.w bit not implemented");
}
// react to cmd.b bit
if (cmd_desc[2] & 0xC) {
bool cond = true;
if ((cmd_desc[2] & 0xC) != 0xC) {
uint16_t br_mask = this->branch_select >> 16;
cond = (this->ch_stat & br_mask) == (this->branch_select & br_mask);
if ((cmd_desc[2] & 0xC) == 0x8) { // branch if cond cleared?
cond = !cond;
}
}
if (cond) {
this->cmd_ptr = READ_DWORD_LE_A(&cmd_desc[8]);
branch_taken = true;
}
}
if (cmd_desc[2] & 0x30) {
ABORT_F("DBDMA: cmd.i bit not implemented");
}
}
// all INPUT and OUTPUT commands update cmd.resCount
if (cmd < 4 && is_writable) {
WRITE_WORD_LE_A(&curr_cmd[12], this->queue_len & 0xFFFFUL);
if (this->cur_cmd < DBDMA_Cmd::STORE_QUAD && is_writable) {
WRITE_WORD_LE_A(&cmd_desc[12], this->queue_len & 0xFFFFUL);
}
if (!branch_taken)
this->cmd_ptr += 16;
this->cmd_in_progress = false;
}
fetch_cmd(this->cmd_ptr, &cmd_struct);
cmd = cmd_struct.cmd_key >> 4;
this->ch_stat &= ~CH_STAT_WAKE; // clear wake bit (DMA spec, 5.5.3.4)
this->ch_stat &= ~CH_STAT_WAKE; /* clear wake bit (DMA spec, 5.5.3.4) */
this->cur_cmd = cmd_struct.cmd_key >> 4;
switch (cmd_struct.cmd_key >> 4) {
case 0: // OUTPUT_MORE
case 1: // OUTPUT_LAST
case 2: // INPUT_MORE
case 3: // INPUT_LAST
switch (this->cur_cmd) {
case DBDMA_Cmd::OUTPUT_MORE:
case DBDMA_Cmd::OUTPUT_LAST:
case DBDMA_Cmd::INPUT_MORE:
case DBDMA_Cmd::INPUT_LAST:
if (cmd_struct.cmd_key & 7) {
LOG_F(ERROR, "Key > 0 not implemented");
break;
}
if (cmd_struct.cmd_bits & 0x3F) {
LOG_F(ERROR, "non-zero i/b/w not implemented");
break;
}
this->queue_data = mmu_get_dma_mem(cmd_struct.address, cmd_struct.req_count, &is_writable);
this->queue_len = cmd_struct.req_count;
this->cmd_ptr += 16;
this->cmd_in_progress = true;
break;
case 4:
case DBDMA_Cmd::STORE_QUAD:
LOG_F(ERROR, "Unsupported DMA Command STORE_QUAD");
break;
case 5:
case DBDMA_Cmd::LOAD_QUAD:
LOG_F(ERROR, "Unsupported DMA Command LOAD_QUAD");
break;
case 6:
case DBDMA_Cmd::NOP:
LOG_F(ERROR, "Unsupported DMA Command NOP");
break;
case 7:
case DBDMA_Cmd::STOP:
this->ch_stat &= ~CH_STAT_ACTIVE;
this->cmd_in_progress = false;
break;
default:
LOG_F(ERROR, "Unsupported DMA command 0x%X", cmd_struct.cmd_key >> 4);
LOG_F(ERROR, "Unsupported DMA command 0x%X", this->cur_cmd);
this->ch_stat |= CH_STAT_DEAD;
this->ch_stat &= ~CH_STAT_ACTIVE;
}
return (cmd_struct.cmd_key >> 4);
return this->cur_cmd;
}
@ -184,6 +207,10 @@ void DMAChannel::reg_write(uint32_t offset, uint32_t value, int size) {
new_stat &= ~CH_STAT_FLUSH;
this->ch_stat = new_stat;
}
// update ch_stat.s0...s7 if requested
if ((new_stat & 0xFF) != (old_stat & 0xFF)) {
this->ch_stat |= new_stat & 0xFF;
}
break;
case DMAReg::CH_STAT:
break; /* ingore writes to ChannelStatus */
@ -193,6 +220,9 @@ void DMAChannel::reg_write(uint32_t offset, uint32_t value, int size) {
LOG_F(9, "CommandPtrLo set to 0x%X", this->cmd_ptr);
}
break;
case DMAReg::BRANCH_SELECT:
this->branch_select = value & 0xFF00FFUL;
break;
default:
LOG_F(WARNING, "Unsupported DMA channel register 0x%X", offset);
}
@ -294,6 +324,8 @@ void DMAChannel::resume() {
void DMAChannel::abort() {
LOG_F(9, "Aborting DMA channel");
if (this->stop_cb)
this->stop_cb();
}
void DMAChannel::pause() {

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -36,9 +36,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** DBDMA Channel registers offsets */
enum DMAReg : uint32_t {
CH_CTRL = 0,
CH_STAT = 4,
CMD_PTR_LO = 12,
CH_CTRL = 0,
CH_STAT = 4,
CMD_PTR_LO = 12,
INT_SELECT = 16,
BRANCH_SELECT = 20,
};
/** Channel Status bits (DBDMA spec, 5.5.3) */
@ -62,6 +64,19 @@ typedef struct DMACmd {
uint16_t xfer_stat;
} DMACmd;
namespace DBDMA_Cmd {
enum : uint8_t {
OUTPUT_MORE = 0,
OUTPUT_LAST = 1,
INPUT_MORE = 2,
INPUT_LAST = 3,
STORE_QUAD = 4,
LOAD_QUAD = 5,
NOP = 6,
STOP = 7
};
};
typedef std::function<void(void)> DbdmaCallback;
class DMAChannel : public DmaBidirChannel {
@ -90,12 +105,14 @@ private:
std::function<void(void)> start_cb = nullptr; // DMA channel start callback
std::function<void(void)> stop_cb = nullptr; // DMA channel stop callback
uint16_t ch_stat = 0;
uint32_t cmd_ptr = 0;
uint32_t queue_len = 0;
uint8_t* queue_data = 0;
uint16_t ch_stat = 0;
uint32_t cmd_ptr = 0;
uint32_t queue_len = 0;
uint8_t* queue_data = 0;
uint32_t branch_select = 0;
bool cmd_in_progress = false;
bool cmd_in_progress = false;
uint8_t cur_cmd;
};
#endif /* DB_DMA_H */

View File

@ -45,6 +45,7 @@ enum HWCompType {
SCSI_DEV = 1ULL << 22, /* SCSI device */
IDE_BUS = 1ULL << 23, /* IDE bus */
IDE_DEV = 1ULL << 25, /* IDE device */
SND_CODEC = 1ULL << 30, /* Sound codec */
SND_SERVER = 1ULL << 31, /* host sound server */
FLOPPY_CTRL = 1ULL << 32, /* floppy disk controller */
FLOPPY_DRV = 1ULL << 33, /* floppy disk drive */

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file Utilities for working with the Apple Open Firmware NVRAM partition. */
/** Utilities for working with Apple Open Firmware and CHRP NVRAM partitions. */
#include <devices/common/ofnvram.h>
#include <endianswap.h>
@ -32,11 +32,19 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <tuple>
using namespace std;
static std::string hex2str(uint32_t n)
{
std::stringstream ss;
ss << setw(8) << setfill('0') << hex << n;
return ss.str();
}
static uint32_t str2env(string& num_str) {
try {
return (uint32_t)stoul(num_str, NULL, 0);
@ -50,22 +58,15 @@ static uint32_t str2env(string& num_str) {
}
}
int OfNvramUtils::init()
{
this->nvram_obj = dynamic_cast<NVram*>(gMachineObj->get_comp_by_name("NVRAM"));
return this->nvram_obj == nullptr;
}
bool OfNvramUtils::validate()
{
int i;
OfNvramHdr hdr;
bool OfConfigAppl::validate() {
int i;
OfConfigHdrAppl hdr;
if (this->nvram_obj == nullptr)
return false;
// read OF partition header
for (i = 0; i < sizeof(OfNvramHdr); i++) {
for (i = 0; i < sizeof(OfConfigHdrAppl); i++) {
((uint8_t*)&hdr)[i] = this->nvram_obj->read_byte(OF_NVRAM_OFFSET + i);
}
@ -75,7 +76,7 @@ bool OfNvramUtils::validate()
this->size = hdr.num_pages * 256;
if (this->size != 0x800)
if (this->size != OF_CFG_SIZE)
return false;
// read the entire partition into the local buffer
@ -88,10 +89,9 @@ bool OfNvramUtils::validate()
return false;
return true;
}
};
uint16_t OfNvramUtils::checksum_partition()
{
uint16_t OfConfigAppl::checksum_partition() {
uint32_t acc = 0;
for (int i = 0; i < this->size; i += 2) {
@ -101,25 +101,6 @@ uint16_t OfNvramUtils::checksum_partition()
return acc + (acc >> 16);
}
void OfNvramUtils::update_partition()
{
// set checksum in the header to zero
this->buf[4] = 0;
this->buf[5] = 0;
// calculate new checksum
uint16_t checksum = this->checksum_partition();
checksum = checksum ? ~checksum : 0xFFFFU;
// stuff it into the header
WRITE_WORD_BE_A(&this->buf[4], checksum);
// write the entire partition back to NVRAM
for (int i = 0; i < this->size; i++) {
this->nvram_obj->write_byte(OF_NVRAM_OFFSET + i, this->buf[i]);
}
}
static const string flag_names[8] = {
"little-endian?",
"real-mode?",
@ -154,58 +135,74 @@ static const map<string, std::tuple<int, uint16_t>> of_vars = {
{"boot-command", {OF_VAR_TYPE_STR, 0x58}},
};
void OfNvramUtils::printenv()
{
int i;
const OfConfigImpl::config_dict& OfConfigAppl::get_config_vars() {
this->_config_vars.clear();
if (!this->validate()) {
cout << "Invalid Open Firmware partition content!" << endl;
return;
}
if (!this->validate())
return _config_vars;
uint8_t of_flags = this->buf[12];
cout << endl;
// print flags
for (i = 0; i < 8; i++) {
cout << setw(20) << left << flag_names[i] <<
(((of_flags << i) & 0x80) ? "true" : "false") << endl;
// populate flags
for (int i = 0; i < 8; i++) {
_config_vars.push_back(std::make_pair(flag_names[i],
((of_flags << i) & 0x80) ? "true" : "false"));
}
// print the remaining variables
// populate the remaining variables
for (auto& var : of_vars) {
auto name = var.first;
auto type = std::get<0>(var.second);
auto offset = std::get<1>(var.second);
cout << setw(20) << left << var.first;
switch (type) {
case OF_VAR_TYPE_INT:
cout << hex << READ_DWORD_BE_A(&this->buf[offset]) << endl;
_config_vars.push_back(std::make_pair(name,
hex2str(READ_DWORD_BE_A(&this->buf[offset]))));
break;
case OF_VAR_TYPE_STR:
uint16_t str_offset = READ_WORD_BE_A(&this->buf[offset]) - OF_NVRAM_OFFSET;
uint16_t str_len = READ_WORD_BE_A(&this->buf[offset+2]);
for (i = 0; i < str_len; i++) {
char ch = *(char *)&(this->buf[str_offset + i]);
if (ch == '\x0D') cout << endl; else cout << ch;
if ((str_offset + str_len) > OF_CFG_SIZE) {
cout << "string property too long - skip it" << endl;
break;
}
cout << endl;
char prop_val[OF_CFG_SIZE] = "";
memcpy(prop_val, &this->buf[str_offset], str_len);
prop_val[str_len] = '\0';
_config_vars.push_back(std::make_pair(name, prop_val));
}
}
cout << endl;
return _config_vars;
};
void OfConfigAppl::update_partition() {
// set checksum in the header to zero
this->buf[4] = 0;
this->buf[5] = 0;
// calculate new checksum
uint16_t checksum = this->checksum_partition();
checksum = checksum ? ~checksum : 0xFFFFU;
// stuff checksum into the header
WRITE_WORD_BE_A(&this->buf[4], checksum);
// write the entire partition back to NVRAM
for (int i = 0; i < this->size; i++) {
this->nvram_obj->write_byte(OF_NVRAM_OFFSET + i, this->buf[i]);
}
}
void OfNvramUtils::setenv(string var_name, string value)
{
bool OfConfigAppl::set_var(std::string& var_name, std::string& value) {
int i, flag;
if (!this->validate()) {
cout << "Invalid Open Firmware partition content!" << endl;
return;
}
if (!this->validate())
return false;
// check if the user tries to change a flag
for (i = 0; i < 8; i++) {
@ -216,7 +213,7 @@ void OfNvramUtils::setenv(string var_name, string value)
flag = 0;
else {
cout << "Invalid property value: " << value << endl;
return;
return false;
}
uint8_t flag_bit = 0x80U >> i;
uint8_t of_flags = this->buf[12];
@ -228,15 +225,14 @@ void OfNvramUtils::setenv(string var_name, string value)
this->buf[12] = of_flags;
this->update_partition();
cout << " ok" << endl; // mimic Forth
return;
return true;
}
}
// see if one of the standard properties should be changed
if (of_vars.find(var_name) == of_vars.end()) {
cout << "Attempt to change unknown variable " << var_name << endl;
return;
return false;
}
auto type = std::get<0>(of_vars.at(var_name));
@ -248,7 +244,7 @@ void OfNvramUtils::setenv(string var_name, string value)
num = str2env(value);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
return;
return false;
}
WRITE_DWORD_BE_A(&this->buf[offset], num);
this->update_partition();
@ -257,7 +253,7 @@ void OfNvramUtils::setenv(string var_name, string value)
uint16_t str_offset = READ_WORD_BE_A(&this->buf[offset]);
uint16_t str_len = READ_WORD_BE_A(&this->buf[offset+2]);
OfNvramHdr *hdr = (OfNvramHdr *)&this->buf[0];
OfConfigHdrAppl *hdr = (OfConfigHdrAppl *)&this->buf[0];
uint16_t here = READ_WORD_BE_A(&hdr->here);
uint16_t top = READ_WORD_BE_A(&hdr->top);
@ -266,11 +262,13 @@ void OfNvramUtils::setenv(string var_name, string value)
uint16_t new_top = top + str_len - value.length();
if (new_top < here) {
cout << "No room in the heap!" << endl;
return;
return false;
}
// remove the old string
std::memmove(&this->buf[top + str_len - OF_NVRAM_OFFSET], &this->buf[top - OF_NVRAM_OFFSET], str_offset - top);
std::memmove(&this->buf[top + str_len - OF_NVRAM_OFFSET],
&this->buf[top - OF_NVRAM_OFFSET], str_offset - top);
for (auto& var : of_vars) {
auto type = std::get<0>(var.second);
auto offset = std::get<1>(var.second);
@ -299,7 +297,271 @@ void OfNvramUtils::setenv(string var_name, string value)
// update physical NVRAM
this->update_partition();
cout << " ok" << endl; // mimic Forth
}
return true;
};
uint8_t OfConfigChrp::checksum_hdr(const uint8_t* data)
{
uint16_t sum = data[0];
for (int i = 2; i < 16; i++) {
sum += data[i];
if (sum >= 256)
sum = (sum + 1) & 0xFFU;
}
return sum;
}
bool OfConfigChrp::validate()
{
int i, pos, len;
uint8_t sig;
bool wip = true;
bool of_part_found = false;
// search the entire 8KB NVRAM for CHRP OF config partition.
// Bail out if an unknown partition or free space is encountered.
// Skip over known partitions.
for (pos = 0; wip && pos < 8192;) {
sig = this->nvram_obj->read_byte(pos);
switch (sig) {
case NVRAM_SIG_OF_ENV:
of_part_found = true;
// fall-through
case NVRAM_SIG_FREE:
wip = false;
break;
case NVRAM_SIG_VPD:
case NVRAM_SIG_DIAG:
case NVRAM_SIG_OF_CFG:
case NVRAM_SIG_MAC_OS:
case NVRAM_SIG_ERR_LOG:
// skip valid partitions we're not interested in
len = (this->nvram_obj->read_byte(pos + 2) << 8) |
this->nvram_obj->read_byte(pos + 3);
if (!len || (len * 16) >= 8192)
break;
pos += len * 16;
break;
default:
wip = false;
}
}
if (!of_part_found)
return false;
OfConfigHdrChrp hdr;
// read OF partition header
for (i = 0; i < sizeof(OfConfigHdrChrp); i++) {
((uint8_t*)&hdr)[i] = this->nvram_obj->read_byte(pos + i);
}
len = BYTESWAP_16(hdr.length) * 16;
// sanity checks
if (hdr.sig != NVRAM_SIG_OF_ENV || len < 16 || len > (4096 + sizeof(OfConfigHdrChrp)))
return false;
// calculate partition header checksum
uint8_t chk_sum = this->checksum_hdr((uint8_t*)&hdr);
if (chk_sum != hdr.checksum)
return false;
len -= sizeof(OfConfigHdrChrp);
pos += sizeof(OfConfigHdrChrp);
this->data_offset = pos;
// read the entire partition into the local buffer
for (i = 0; i < len; i++) {
this->buf[i] = this->nvram_obj->read_byte(pos + i);
}
return true;
}
const OfConfigImpl::config_dict& OfConfigChrp::get_config_vars() {
int len;
this->_config_vars.clear();
if (!this->validate())
return _config_vars;
for (int pos = 0; pos < 4096;) {
char *pname = (char *)&this->buf[pos];
// scan property name until '=' is encountered
// or max length is reached
for (len = 0; len < 32; pos++, len++) {
if (pname[len] == '=' || pname[len] == '\0')
break;
}
// empty property name -> free space reached
if (!len) {
this->data_length = pos;
break;
}
if (pname[len] != '=') {
cout << "no = sign found or name > 31 chars" << endl;
break;
}
char prop_name[32];
memcpy(prop_name, pname, len);
prop_name[len] = '\0';
pos++; // skip past '='
char *pval = (char *)&this->buf[pos];
// determine property value length
for (len = 0; pos < 4096; pos++, len++) {
if (pval[len] == '\0')
break;
}
// ensure each property value is null-terminated
if (pos >= 4096) {
cout << "ran off partition end" << endl;
break;
}
this->_config_vars.push_back(std::make_pair(prop_name, pval));
pos++; // skip past null terminator
}
return this->_config_vars;
}
bool OfConfigChrp::update_partition() {
unsigned pos = 0;
memset(this->buf, 0, 4096);
for (auto& var : this->_config_vars) {
if ((var.first.length() + var.second.length() + 2) >= 4096) {
cout << "No room in the partition!" << endl;
return false;
}
memcpy(&this->buf[pos], var.first.c_str(), var.first.length());
pos += var.first.length();
this->buf[pos++] = '=';
memcpy(&this->buf[pos], var.second.c_str(), var.second.length());
pos += var.second.length();
this->buf[pos++] = '\0';
}
// write the entire partition back to NVRAM
for (int i = 0; i < 4096; i++) {
this->nvram_obj->write_byte(this->data_offset + i, this->buf[i]);
}
return true;
}
bool OfConfigChrp::set_var(std::string& var_name, std::string& value) {
if (!this->validate())
return false;
bool found = false;
// see if the user tries to change an existing property
for (auto& var : this->_config_vars) {
if (var.first == var_name) {
found = true;
// see if we're about to change a flag
if (var_name.back() == '?') {
if (value != "true" && value != "false") {
cout << "Flag value can be 'true' or 'false'" << endl;
return false;
}
}
if (value.length() > var.second.length()) {
unsigned free_space = 4096 - this->data_length;
if ((value.length() - var.second.length()) >= free_space) {
cout << "No room for new data!" << endl;
return false;
}
}
var.second = value;
break;
}
}
if (!found) {
cout << "Attempt to change unknown variable " << var_name << endl;
return false;
}
return this->update_partition();
};
int OfConfigUtils::init()
{
this->nvram_obj = dynamic_cast<NVram*>(gMachineObj->get_comp_by_name("NVRAM"));
return this->nvram_obj == nullptr;
}
bool OfConfigUtils::open_container() {
OfConfigImpl* cfg_obj;
if (this->cfg_impl == nullptr) {
cfg_obj = new OfConfigAppl(this->nvram_obj);
if (cfg_obj->validate()) {
this->cfg_impl = std::unique_ptr<OfConfigImpl>(cfg_obj);
return true;
} else {
delete(cfg_obj);
cfg_obj = new OfConfigChrp(this->nvram_obj);
if (cfg_obj->validate()) {
this->cfg_impl = std::unique_ptr<OfConfigImpl>(cfg_obj);
return true;
} else {
delete(cfg_obj);
}
}
} else {
return this->cfg_impl->validate();
}
cout << "No valid Open Firmware partition found!" << endl;
return false;
}
void OfConfigUtils::printenv() {
OfConfigImpl::config_dict vars;
if (!this->open_container())
return;
vars = this->cfg_impl->get_config_vars();
for (auto& var : vars) {
cout << setw(34) << left << var.first << var.second << endl;
}
}
void OfConfigUtils::setenv(string var_name, string value)
{
if (!this->open_container())
return;
if (!this->cfg_impl->set_var(var_name, value)) {
cout << " Please try again" << endl;
} else {
cout << " ok" << endl; // mimic Forth
}
}

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-22 divingkatae and maximum
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -19,20 +19,30 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Apple/CHRP Open Firmware NVRAM partition definitions. */
#ifndef OF_NVRAM_H
#define OF_NVRAM_H
#include <devices/common/nvram.h>
/** @file Utilities for working with the Apple Open Firmware NVRAM partition. */
#include <cinttypes>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
/** ========== Apple Open Firmware 1.x/2.x partition definitions. ========== */
#define OF_NVRAM_OFFSET 0x1800
#define OF_NVRAM_SIG 0x1275
#define OF_CFG_SIZE 0x800
// OF Variable types
enum {
OF_VAR_TYPE_INT = 1,
OF_VAR_TYPE_STR = 2,
OF_VAR_TYPE_INT = 1, // integer
OF_VAR_TYPE_STR = 2, // string
OF_VAR_TYPE_FLG = 3, // boolean flag
};
typedef struct {
@ -42,26 +52,97 @@ typedef struct {
uint16_t checksum; // >nv.checksum partition checksum
uint16_t here; // >nv.here offset to the next free byte (offset of after last string length; = 0x185c)
uint16_t top; // >nv.top offset to the last free byte (offset of string with lowest offset; < 0x2000)
} OfNvramHdr;
} OfConfigHdrAppl;
class OfNvramUtils {
/** ================== CHRP NVRAM partition definitions. ================== */
/** CHRP NVRAM partition signatures. */
enum {
NVRAM_SIG_OF_CFG = 0x50, // of-config (unused in Power Macintosh)
NVRAM_SIG_VPD = 0x52, // of-vpd (unused in Power Macintosh)
NVRAM_SIG_DIAG = 0x5F, // diagnostics partition
NVRAM_SIG_OF_ENV = 0x70, // common partition with OF environment vars
NVRAM_SIG_ERR_LOG = 0x72, // post-err-log
NVRAM_SIG_FREE = 0x7F, // free space
NVRAM_SIG_MAC_OS = 0xA0, // APL,MacOS75
};
typedef struct {
uint8_t sig; // partition signature, must be 0x70
uint8_t checksum; // partition header checksum (sig, length & name)
uint16_t length; // partition length in 16-byte blocks
char name[12]; // null-terminated partition name
} OfConfigHdrChrp;
// - interface
class OfConfigImpl {
public:
OfNvramUtils() = default;
~OfNvramUtils() = default;
using config_dict = std::vector<std::pair<std::string, std::string>>;
virtual ~OfConfigImpl() = default;
virtual bool validate() = 0;
virtual const config_dict& get_config_vars() = 0;
virtual bool set_var(std::string& name, std::string& val) = 0;
};
// Old World implementation
class OfConfigAppl : public OfConfigImpl {
public:
OfConfigAppl(NVram* nvram_obj) { this->nvram_obj = nvram_obj; };
~OfConfigAppl() = default;
bool validate();
const config_dict& get_config_vars();
bool set_var(std::string& var_name, std::string& value);
protected:
uint16_t checksum_partition();
void update_partition();
private:
NVram* nvram_obj = nullptr;
uint8_t buf[OF_CFG_SIZE];
int size = 0;
config_dict _config_vars;
};
// New World implementation
class OfConfigChrp : public OfConfigImpl {
public:
OfConfigChrp(NVram* nvram_obj) { this->nvram_obj = nvram_obj; };
~OfConfigChrp() = default;
bool validate();
const config_dict& get_config_vars();
bool set_var(std::string& var_name, std::string& value);
protected:
uint8_t checksum_hdr(const uint8_t* data);
bool update_partition();
private:
NVram* nvram_obj = nullptr;
uint8_t buf[4096];
unsigned data_offset = 0; // offset to the OF config data
unsigned data_length = 0; // length of the OF config data
config_dict _config_vars;
};
class OfConfigUtils {
public:
OfConfigUtils() = default;
~OfConfigUtils() = default;
int init();
void printenv();
void setenv(std::string var_name, std::string value);
protected:
bool validate();
uint16_t checksum_partition();
void update_partition();
bool open_container();
private:
NVram* nvram_obj = nullptr;
int size = 0;
uint8_t buf[0x800];
std::unique_ptr<OfConfigImpl> cfg_impl = nullptr;
NVram* nvram_obj = nullptr;
};
#endif // OF_NVRAM_H

View File

@ -0,0 +1,94 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** DEC 21154 PCI-to-PCI bridge emulation. */
#include <devices/common/hwcomponent.h>
#include <devices/common/pci/dec21154.h>
#include <devices/deviceregistry.h>
#include <endianswap.h>
#include <loguru.hpp>
#include <cinttypes>
DecPciBridge::DecPciBridge(std::string name) : PCIBridge(name)
{
supports_types(HWCompType::PCI_HOST | HWCompType::PCI_DEV);
// initialize PCI config header
this->vendor_id = PCI_VENDOR_DEC;
this->device_id = 0x0026;
this->class_rev = 0x06040002;
this->cache_ln_sz = 0;
this->command = 0;
this->status = 0x2B0;
}
uint32_t DecPciBridge::pci_cfg_read(uint32_t reg_offs, AccessDetails &details)
{
if (reg_offs < 64) {
return PCIBridge::pci_cfg_read(reg_offs, details);
}
switch (reg_offs) {
case CHIP_CTRL:
return (this->arb_ctrl << 16) | (this-> diag_ctrl << 8) | this->chip_ctrl;
case PSERR_EVENT_DIS:
return (this->gpio_out_en << 16) | this->pserr_event_dis;
case SEC_CLK_CTRL:
return this->sec_clock_ctrl;
default:
LOG_READ_UNIMPLEMENTED_CONFIG_REGISTER();
}
return 0;
}
void DecPciBridge::pci_cfg_write(uint32_t reg_offs, uint32_t value, AccessDetails &details)
{
if (reg_offs < 64) {
PCIBridge::pci_cfg_write(reg_offs, value, details);
return;
}
switch (reg_offs) {
case CHIP_CTRL:
this->chip_ctrl = value & 0xFFU;
this->diag_ctrl = (value >> 8) & 0xFFU;
this->arb_ctrl = value >> 16;
break;
case PSERR_EVENT_DIS:
this->pserr_event_dis = value & 0xFFU;
this->gpio_out_en = (value >> 16) & 0xFFU;
break;
case SEC_CLK_CTRL:
this->sec_clock_ctrl = value & 0xFFFFU;
break;
default:
LOG_WRITE_UNIMPLEMENTED_CONFIG_REGISTER();
}
}
static const DeviceDescription Dec21154_Descriptor = {
DecPciBridge::create, {}, {}
};
REGISTER_DEVICE(Dec21154, Dec21154_Descriptor);

View File

@ -0,0 +1,62 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** DEC 21154 PCI-to-PCI bridge definitions. */
#ifndef DEC_P2P_H
#define DEC_P2P_H
#include <devices/common/pci/pcibridge.h>
#include <cinttypes>
#include <string>
enum {
CHIP_CTRL = 0x40,
PSERR_EVENT_DIS = 0x64,
SEC_CLK_CTRL = 0x68,
};
class DecPciBridge : public PCIBridge {
public:
DecPciBridge(std::string name);
~DecPciBridge() = default;
static std::unique_ptr<HWComponent> create() {
return std::unique_ptr<DecPciBridge>(new DecPciBridge("DEC21154"));
}
// PCIDevice methods
uint32_t pci_cfg_read(uint32_t reg_offs, AccessDetails &details);
void pci_cfg_write(uint32_t reg_offs, uint32_t value, AccessDetails &details);
private:
uint8_t chip_ctrl = 0;
uint8_t diag_ctrl = 0;
uint16_t arb_ctrl = 0x0200;
uint8_t pserr_event_dis = 0;
uint8_t gpio_out_data = 0;
uint8_t gpio_out_en = 0;
uint8_t gpio_in_data = 0;
uint16_t sec_clock_ctrl = 0;
};
#endif // DEC_P2P_H

View File

@ -54,6 +54,7 @@ enum {
/** PCI Vendor IDs for devices used in Power Macintosh computers. */
enum {
PCI_VENDOR_ATI = 0x1002,
PCI_VENDOR_DEC = 0x1011,
PCI_VENDOR_MOTOROLA = 0x1057,
PCI_VENDOR_APPLE = 0x106B,
PCI_VENDOR_NVIDIA = 0x10DE,

View File

@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <devices/deviceregistry.h>
#include <endianswap.h>
#include <array>
#include <cinttypes>
#include <string>
#include <unordered_map>
@ -78,6 +79,13 @@ public:
virtual PCIDevice *pci_find_device(uint8_t bus_num, uint8_t dev_num, uint8_t fun_num);
virtual uint32_t pci_t1_read(uint8_t dev, uint32_t fun, uint32_t reg, AccessDetails &details) {
return 0;
};
virtual void pci_t1_write(uint8_t dev, uint32_t fun, uint32_t reg, uint32_t value,
AccessDetails &details) {};
protected:
std::unordered_map<int, PCIDevice*> dev_map;
std::vector<PCIDevice*> io_space_devs;

View File

@ -235,7 +235,7 @@ void AMIC::write(uint32_t rgn_start, uint32_t offset, uint32_t value, int size)
this->snd_buf_size);
this->snd_out_dma->enable();
this->awacs->set_sample_rate((this->snd_out_ctrl >> 1) & 3);
this->awacs->start_output_dma();
this->awacs->dma_out_start();
} else {
LOG_F(9, "AMIC Sound Out DMA disabled!");
this->snd_out_dma->disable();

View File

@ -60,8 +60,8 @@ GrandCentral::GrandCentral() : PCIDevice("mac-io/grandcentral"), InterruptCtrl()
this->snd_out_dma = std::unique_ptr<DMAChannel> (new DMAChannel());
this->awacs->set_dma_out(this->snd_out_dma.get());
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->awacs.get()),
std::bind(&AwacsScreamer::dma_end, this->awacs.get())
std::bind(&AwacsScreamer::dma_out_start, this->awacs.get()),
std::bind(&AwacsScreamer::dma_out_stop, this->awacs.get())
);
// connect serial HW

View File

@ -67,13 +67,14 @@ HeathrowIC::HeathrowIC() : PCIDevice("mac-io/heathrow"), InterruptCtrl()
// connect Cuda
this->viacuda = dynamic_cast<ViaCuda*>(gMachineObj->get_comp_by_name("ViaCuda"));
// initialize sound chip and its DMA output channel, then wire them together
this->screamer = std::unique_ptr<AwacsScreamer> (new AwacsScreamer());
// find appropriate sound chip, create a DMA output channel for sound,
// then wire everything together
this->snd_codec = dynamic_cast<MacioSndCtrl*>(gMachineObj->get_comp_by_type(HWCompType::SND_CODEC));
this->snd_out_dma = std::unique_ptr<DMAChannel> (new DMAChannel());
this->screamer->set_dma_out(this->snd_out_dma.get());
this->snd_codec->set_dma_out(this->snd_out_dma.get());
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->screamer.get()),
std::bind(&AwacsScreamer::dma_end, this->screamer.get())
std::bind(&AwacsScreamer::dma_out_start, this->snd_codec),
std::bind(&AwacsScreamer::dma_out_stop, this->snd_codec)
);
// connect SCSI HW
@ -102,7 +103,7 @@ void HeathrowIC::notify_bar_change(int bar_num)
if (this->base_addr != (this->bars[bar_num] & 0xFFFFFFF0UL)) {
if (this->base_addr) {
LOG_F(WARNING, "Heathrow: deallocating I/O memory not implemented");
this->host_instance->pci_unregister_mmio_region(this->base_addr, 0x80000, this);
}
this->base_addr = this->bars[0] & 0xFFFFFFF0UL;
this->host_instance->pci_register_mmio_region(this->base_addr, 0x80000, this);
@ -162,7 +163,7 @@ uint32_t HeathrowIC::read(uint32_t rgn_start, uint32_t offset, int size) {
case 0x13: // ESCC MacRISC addressing
return this->escc->read((offset >> 4) & 0xF);
case 0x14:
res = this->screamer->snd_ctrl_read(offset - 0x14000, size);
res = this->snd_codec->snd_ctrl_read(offset - 0x14000, size);