-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,130 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// SPDX-FileCopyrightText: 2024 ergo720 | ||
|
||
#include "machine.hpp" | ||
#include <cinttypes> | ||
|
||
#define MODULE_NAME smc | ||
|
||
#define SMC_VERSION_STR 0x01 | ||
#define SMC_TRAY_STATE 0x03 | ||
#define SMC_VIDEO_MODE 0x04 | ||
#define SMC_FAN_MODE 0x05 | ||
#define SMC_FAN_SPEED 0x06 | ||
#define SMC_LED_OVERRIDE 0x07 | ||
#define SMC_LED_STATES 0x08 | ||
#define SMC_WRITE_SCRATCH 0x0E | ||
#define SMC_READ_SCRATCH 0x0F | ||
#define SMC_READ_FAN_SPEED 0x10 | ||
#define SMC_SCRATCH 0x1B | ||
|
||
#define SMC_TRAY_STATE_OPEN 0x10 | ||
#define SMC_TRAY_STATE_NO_MEDIA 0x40 | ||
#define SMC_TRAY_STATE_MEDIA_DETECT 0x60 | ||
|
||
#define SMC_VIDEO_MODE_SCART 0x00 | ||
#define SMC_VIDEO_MODE_HDTV 0x01 | ||
#define SMC_VIDEO_MODE_VGA 0x02 | ||
#define SMC_VIDEO_MODE_RFU 0x03 | ||
#define SMC_VIDEO_MODE_SVIDEO 0x04 | ||
#define SMC_VIDEO_MODE_STANDARD 0x06 | ||
#define SMC_VIDEO_MODE_NONE 0x07 | ||
|
||
|
||
std::optional<uint16_t> | ||
smc::read_byte(uint8_t command) | ||
{ | ||
uint8_t value; | ||
switch (command) | ||
{ | ||
case SMC_VERSION_STR: | ||
value = m_version[m_version_idx]; | ||
break; | ||
|
||
case SMC_TRAY_STATE: | ||
value = m_tray_state.load(); | ||
break; | ||
|
||
case SMC_VIDEO_MODE: | ||
case SMC_SCRATCH: | ||
value = m_regs[command]; | ||
break; | ||
|
||
case SMC_READ_SCRATCH: | ||
value = m_regs[SMC_WRITE_SCRATCH]; | ||
break; | ||
|
||
case SMC_READ_FAN_SPEED: | ||
value = (m_regs[SMC_FAN_MODE] == 1) ? m_regs[SMC_FAN_SPEED] : 0; | ||
break; | ||
|
||
default: | ||
nxbx_fatal("Unhandled read with command 0x%" PRIX8, command); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
std::optional<uint16_t> | ||
smc::write_byte(uint8_t command, uint8_t data) | ||
{ | ||
switch (command) | ||
{ | ||
case SMC_VERSION_STR: | ||
m_version_idx = (data == 0) ? 0 : m_version_idx; | ||
break; | ||
|
||
case SMC_FAN_MODE: | ||
m_regs[command] = data & 1; | ||
break; | ||
|
||
case SMC_FAN_SPEED: | ||
m_regs[command] = (data > 50) ? m_regs[command] : data; | ||
break; | ||
|
||
case SMC_LED_OVERRIDE: // TODO: display on the gui somehow | ||
m_regs[command] = data & 1; | ||
if (m_regs[command] == 0) { | ||
m_regs[SMC_LED_STATES] = 0x0F; // solid green | ||
} | ||
break; | ||
|
||
case SMC_LED_STATES: | ||
m_regs[command] = data; | ||
break; | ||
|
||
default: | ||
nxbx_fatal("Unhandled write with command 0x%" PRIX8 " and value 0x%" PRIX8, command, data); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void | ||
smc::update_tray_state(::tray_state state, bool do_int) | ||
{ | ||
m_tray_state = (uint8_t)state; | ||
if (do_int) { | ||
// TODO: trigger interrupt | ||
nxbx_fatal("Interrupts not supported yet"); | ||
} | ||
} | ||
|
||
void | ||
smc::reset() | ||
{ | ||
std::fill(std::begin(m_regs), std::end(m_regs), 0); | ||
m_regs[SMC_LED_STATES] = 0x0F; // solid green | ||
m_version_idx = 0; | ||
} | ||
|
||
bool | ||
smc::init() | ||
{ | ||
reset(); | ||
m_regs[SMC_TRAY_STATE] = SMC_TRAY_STATE_MEDIA_DETECT; // TODO: should change state when the user boots new XBEs/XISOs from the gui | ||
m_regs[SMC_VIDEO_MODE] = SMC_VIDEO_MODE_HDTV; // TODO: make configurable | ||
|
||
return true; | ||
} |
This file contains 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,35 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// SPDX-FileCopyrightText: 2024 ergo720 | ||
|
||
#pragma once | ||
|
||
#include "smbus.hpp" | ||
#include <atomic> | ||
#include <cstdint> | ||
|
||
|
||
// NOTE: same state values as used internally by the smc, to avoid conversions | ||
enum class tray_state : uint8_t { | ||
open = 0x10, | ||
no_media = 0x40, | ||
media_detect = 0x60, | ||
}; | ||
|
||
class smc : public smbus_device { | ||
public: | ||
smc(log_module module_name) : smbus_device(module_name) {} | ||
bool init(); | ||
void deinit() override {} | ||
void reset(); | ||
std::optional<uint16_t> read_byte(uint8_t command) override; | ||
std::optional<uint16_t> write_byte(uint8_t command, uint8_t data) override; | ||
void update_tray_state(tray_state state, bool do_int); | ||
|
||
private: | ||
//machine *const m_machine; | ||
static constexpr uint8_t m_version[3] = { 'P', '0', '5' }; | ||
uint8_t m_version_idx; | ||
uint8_t m_regs[34]; | ||
std::atomic_uint8_t m_tray_state; // atomic because it can be updated by console::update_tray_state | ||
}; |
This file contains 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 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