Skip to content
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

[WIP] Bacon Flasher for my carts #113

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/__pycache__/
FlashGBX/config/*bak
4 changes: 2 additions & 2 deletions FlashGBX/FlashGBX_CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from .PocketCamera import PocketCamera
from .Util import APPNAME, ANSI
from . import Util
from . import hw_GBxCartRW, hw_GBFlash, hw_JoeyJr
hw_devices = [hw_GBxCartRW, hw_GBFlash, hw_JoeyJr]
from . import hw_Bacon, hw_GBxCartRW, hw_GBFlash, hw_JoeyJr
hw_devices = [hw_Bacon, hw_GBxCartRW, hw_GBFlash, hw_JoeyJr]

class FlashGBX_CLI():
ARGS = {}
Expand Down
4 changes: 2 additions & 2 deletions FlashGBX/FlashGBX_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from .UserInputDialog import UserInputDialog
from .Util import APPNAME, VERSION, VERSION_PEP440
from . import Util
from . import hw_GBxCartRW, hw_GBFlash, hw_JoeyJr
hw_devices = [hw_GBxCartRW, hw_GBFlash, hw_JoeyJr]
from . import hw_Bacon, hw_GBxCartRW, hw_GBFlash, hw_JoeyJr
hw_devices = [hw_Bacon, hw_GBxCartRW, hw_GBFlash, hw_JoeyJr]

class FlashGBX_GUI(QtWidgets.QWidget):
CONN = None
Expand Down
26 changes: 21 additions & 5 deletions FlashGBX/LK_Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def _cart_write_flash(self, commands, flashcart=False):
buffer.extend(struct.pack("B", 1 if flashcart else 0))
buffer.extend(struct.pack("B", num))
for i in range(0, num):
dprint("Writing to cartridge: 0x{:X} = 0x{:X} ({:d} of {:d})".format(commands[i][0], commands[i][1], i+1, num))
dprint("Writing to cartridge: 0x{:X} = 0x{:X} ({:d} of {:d}) flashcart={:s}".format(commands[i][0], commands[i][1], i+1, num, str(flashcart)))
if self.MODE == "AGB" and flashcart:
buffer.extend(struct.pack(">I", commands[i][0] >> 1))
else:
Expand Down Expand Up @@ -919,6 +919,7 @@ def ReadInfo(self, setPinsAsInputs=False, checkRtc=True):
print("{:s}Error: No mode was set.{:s}".format(ANSI.RED, ANSI.RESET))
return False

dprint("Reading ROM header")
header = self.ReadROM(0, 0x180)

if ".dev" in Util.VERSION_PEP440 or Util.DEBUG:
Expand Down Expand Up @@ -1036,7 +1037,7 @@ def ReadInfo(self, setPinsAsInputs=False, checkRtc=True):
data["rtc_string"] = "Not available"
if checkRtc and data["logo_correct"] is True and header[0xC5] == 0 and header[0xC7] == 0 and header[0xC9] == 0:
_agb_gpio = AGB_GPIO(args={"rtc":True}, cart_write_fncptr=self._cart_write, cart_read_fncptr=self._cart_read, cart_powercycle_fncptr=self.CartPowerCycleOrAskReconnect, clk_toggle_fncptr=self._clk_toggle)
if self.FW["fw_ver"] >= 12:
if self.FW["fw_ver"] >= 12 and self.DEVICE_NAME != "Bacon": # Bacon has a different RTC implementation
self._write(self.DEVICE_CMD["AGB_READ_GPIO_RTC"])
temp = self._read(8)
data["has_rtc"] = _agb_gpio.HasRTC(temp) is True
Expand Down Expand Up @@ -1424,6 +1425,8 @@ def ReadFlashSaveID(self):
return (agb_flash_chip, agb_flash_chip_name)

def ReadROM(self, address, length, skip_init=False, max_length=64):
if self.DEVICE_NAME == "Bacon":
max_length = length
num = math.ceil(length / max_length)
dprint("Reading 0x{:X} bytes from cartridge ROM at 0x{:X} in {:d} iteration(s)".format(length, address, num))
if length > max_length: length = max_length
Expand Down Expand Up @@ -1491,6 +1494,8 @@ def ReadROM_GBAMP(self, address, length, max_length=64):
return self.ReadROM(address=addr, length=length, max_length=max_length)

def ReadRAM(self, address, length, command=None, max_length=64):
if self.DEVICE_NAME == "Bacon":
max_length = length
num = math.ceil(length / max_length)
dprint("Reading 0x{:X} bytes from cartridge RAM in {:d} iteration(s)".format(length, num))
if length > max_length: length = max_length
Expand Down Expand Up @@ -1577,6 +1582,8 @@ def ReadRAM_TAMA5(self):

def WriteRAM(self, address, buffer, command=None, max_length=256):
length = len(buffer)
if self.DEVICE_NAME == "Bacon":
max_length = length
num = math.ceil(length / max_length)
dprint("Writing 0x{:X} bytes to cartridge RAM in {:d} iteration(s)".format(length, num))
if length > max_length: length = max_length
Expand Down Expand Up @@ -1702,8 +1709,10 @@ def WriteRAM_TAMA5(self, buffer):

def WriteROM(self, address, buffer, flash_buffer_size=False, skip_init=False, rumble_stop=False, max_length=MAX_BUFFER_WRITE):
length = len(buffer)
if self.DEVICE_NAME == "Bacon":
max_length = length
num = math.ceil(length / max_length)
dprint("Writing 0x{:X} bytes to Flash ROM in {:d} iteration(s)".format(length, num))
dprint("Writing 0x{:X} bytes to Flash ROM in {:d} iteration(s) flash_buffer_size=0x{:X} skip_init={:s}".format(length, num, flash_buffer_size, str(skip_init)))
if length == 0:
dprint("Length is zero?")
return False
Expand Down Expand Up @@ -3401,7 +3410,7 @@ def _BackupRestoreRAM(self, args):
elif self.MODE == "AGB":
_agb_gpio = AGB_GPIO(args={"rtc":True}, cart_write_fncptr=self._cart_write, cart_read_fncptr=self._cart_read, cart_powercycle_fncptr=self.CartPowerCycleOrAskReconnect, clk_toggle_fncptr=self._clk_toggle)
rtc_buffer = None
if self.FW["fw_ver"] >= 12:
if self.FW["fw_ver"] >= 12 and self.DEVICE_NAME != "Bacon": # Bacon has a different RTC implementation
self._write(self.DEVICE_CMD["AGB_READ_GPIO_RTC"])
rtc_buffer = self._read(8)
if len(rtc_buffer) == 8 and _agb_gpio.HasRTC(rtc_buffer) is True:
Expand Down Expand Up @@ -4287,6 +4296,8 @@ def _FlashROM(self, args):
sector_size = se_ret
dprint("Next sector size: 0x{:X}".format(sector_size))
skip_init = False
if "Bacon" in self.DEVICE_NAME:
self.DEVICE.cache_rom(pos, [0xFF]*buffer_len)
# ↑↑↑ Sector erase

if se_ret is not False:
Expand Down Expand Up @@ -4646,7 +4657,12 @@ def TransferData(self, args, signal):
if args['mode'] == 1: ret = self._BackupROM(args)
elif args['mode'] == 2: ret = self._BackupRestoreRAM(args)
elif args['mode'] == 3: ret = self._BackupRestoreRAM(args)
elif args['mode'] == 4: ret = self._FlashROM(args)
elif args['mode'] == 4:
if "Bacon" in self.DEVICE_NAME:
self.DEVICE.cache_rom_reset()
ret = self._FlashROM(args)
if "Bacon" in self.DEVICE_NAME:
self.DEVICE.cache_rom_reset()
elif args['mode'] == 5: ret = self._DetectCartridge(args)
elif args['mode'] == 0xFF: self.Debug()
if self.FW is None: return False
Expand Down
2 changes: 2 additions & 0 deletions FlashGBX/bacon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/__pycache__/
gba*.bin
55 changes: 55 additions & 0 deletions FlashGBX/bacon/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
CH347 Module
------------

The `ch347` module provides functions for interacting with the CH347 USB-UART/SPI/I2C/JTAG bridge.

Initialization:
----------------

- Use `ch347.init()` to initialize the CH347 device.

Configuration:
--------------

- Use `ch347.set_spi_config(clock: int, mode: int)` to configure the SPI parameters.

SPI Communication:
------------------

- Use `ch347.spi_write(device_index: int, chip_select: int, write_data: bytes, write_step: int)`
to write data to the SPI bus.

- Use `ch347.spi_read(device_index: int, chip_select: int, write_data: bytes, read_length: int)`
to read data from the SPI bus.

Closing:
--------

- Use `ch347.close()` to close the CH347 device.

Usage Example:
--------------

import ch347

# Initialize the CH347 device
ch347.init()

# Configure the SPI parameters
ch347.set_spi_config(clock=1000000, mode=0)

# Write data to the SPI bus
ch347.spi_write(device_index=0, chip_select=1, write_data=b'\x01\x02\x03', write_step=3)

# Read data from the SPI bus
data = ch347.spi_read(device_index=0, chip_select=1, write_data=b'\x00', read_length=3)

# Close the CH347 device
ch347.close()
"""

from .ch347 import *
from .bacon import *
from .command import *
from .serial import *
Loading