N64FlashcartMenu/src/boot/boot.c

143 lines
4.0 KiB
C
Raw Normal View History

#include <libdragon.h>
#include "boot_io.h"
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
#include "boot.h"
#include "cic.h"
#define C0_STATUS_FR (1 << 26)
#define C0_STATUS_CU0 (1 << 28)
#define C0_STATUS_CU1 (1 << 29)
extern uint32_t reboot_start __attribute__((section(".text")));
extern size_t reboot_size __attribute__((section(".text")));
static io32_t *boot_get_device_base (boot_params_t *params) {
io32_t *device_base_address = ROM_CART;
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
if (params->device_type == BOOT_DEVICE_TYPE_64DD) {
device_base_address = ROM_DDIPL;
}
return device_base_address;
}
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
static void boot_detect_cic_seed (boot_params_t *params) {
io32_t *base = boot_get_device_base(params);
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
uint8_t ipl3[IPL3_LENGTH] __attribute__((aligned(8)));
data_cache_hit_writeback_invalidate(ipl3, sizeof(ipl3));
dma_read_raw_async(ipl3, (uint32_t) (&base[16]), sizeof(ipl3));
dma_wait();
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
params->cic_seed = cic_get_seed(cic_detect(ipl3));
}
void boot (boot_params_t *params) {
if (params->tv_type == BOOT_TV_TYPE_PASSTHROUGH) {
params->tv_type = OS_INFO->tv_type;
}
if (params->detect_cic_seed) {
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
boot_detect_cic_seed(params);
}
OS_INFO->mem_size_6105 = OS_INFO->mem_size;
C0_WRITE_STATUS(C0_STATUS_CU1 | C0_STATUS_CU0 | C0_STATUS_FR);
while (!(cpu_io_read(&SP->SR) & SP_SR_HALT));
2023-09-06 00:49:06 +02:00
cpu_io_write(&SP->SR,
SP_SR_CLR_SIG7 |
SP_SR_CLR_SIG6 |
SP_SR_CLR_SIG5 |
SP_SR_CLR_SIG4 |
SP_SR_CLR_SIG3 |
SP_SR_CLR_SIG2 |
SP_SR_CLR_SIG1 |
SP_SR_CLR_SIG0 |
SP_SR_CLR_INTR_BREAK |
SP_SR_CLR_SSTEP |
SP_SR_CLR_INTR |
SP_SR_CLR_BROKE |
SP_SR_SET_HALT
);
cpu_io_write(&SP->SEMAPHORE, 0);
cpu_io_write(&SP->PC, 0);
while (cpu_io_read(&SP->DMA_BUSY));
cpu_io_write(&PI->SR, PI_SR_CLR_INTR | PI_SR_RESET);
2023-08-27 18:52:03 +02:00
while ((cpu_io_read(&VI->CURR_LINE) & ~(VI_CURR_LINE_FIELD)) != 0);
cpu_io_write(&VI->V_INTR, 0x3FF);
cpu_io_write(&VI->H_LIMITS, 0);
cpu_io_write(&VI->CURR_LINE, 0);
cpu_io_write(&AI->MADDR, 0);
cpu_io_write(&AI->LEN, 0);
while (cpu_io_read(&SP->SR) & SP_SR_DMA_BUSY);
uint32_t *reboot_src = &reboot_start;
io32_t *reboot_dst = SP_MEM->IMEM;
size_t reboot_instructions = (size_t) (&reboot_size) / sizeof(uint32_t);
for (int i = 0; i < reboot_instructions; i++) {
cpu_io_write(&reboot_dst[i], reboot_src[i]);
}
cpu_io_write(&PI->DOM[0].LAT, 0xFF);
cpu_io_write(&PI->DOM[0].PWD, 0xFF);
cpu_io_write(&PI->DOM[0].PGS, 0x0F);
cpu_io_write(&PI->DOM[0].RLS, 0x03);
io32_t *base = boot_get_device_base(params);
uint32_t pi_config = io_read((uint32_t) (base));
cpu_io_write(&PI->DOM[0].LAT, pi_config & 0xFF);
cpu_io_write(&PI->DOM[0].PWD, pi_config >> 8);
cpu_io_write(&PI->DOM[0].PGS, pi_config >> 16);
cpu_io_write(&PI->DOM[0].RLS, pi_config >> 20);
if (cpu_io_read(&DPC->SR) & DPC_SR_XBUS_DMEM_DMA) {
while (cpu_io_read(&DPC->SR) & DPC_SR_PIPE_BUSY);
}
io32_t *ipl3_src = base;
io32_t *ipl3_dst = SP_MEM->DMEM;
for (int i = 16; i < 1024; i++) {
cpu_io_write(&ipl3_dst[i], io_read((uint32_t) (&ipl3_src[i])));
}
register uint32_t boot_device asm ("s3");
register uint32_t tv_type asm ("s4");
register uint32_t reset_type asm ("s5");
register uint32_t cic_seed asm ("s6");
register uint32_t version asm ("s7");
boot_device = (params->device_type & 0x01);
tv_type = (params->tv_type & 0x03);
64DD disk loading support for SummerCart64 (#49) (and other fixes/improvements) <!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Motivation and Context <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: GITHUB_USER <GITHUB_USER_EMAIL> --------- Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl> Co-authored-by: Robin Jones <networkfusion@users.noreply.github.com>
2023-10-10 21:12:53 +02:00
reset_type = BOOT_RESET_TYPE_NMI;
cic_seed = (params->cic_seed & 0xFF);
2023-08-10 21:25:54 +02:00
version = (params->tv_type == BOOT_TV_TYPE_PAL) ? 6
: (params->tv_type == BOOT_TV_TYPE_NTSC) ? 1
: (params->tv_type == BOOT_TV_TYPE_MPAL) ? 4
: 0;
asm volatile (
2023-09-04 23:31:19 +02:00
"la $t3, reboot \n"
"jr $t3 \n" ::
[boot_device] "r" (boot_device),
[tv_type] "r" (tv_type),
[reset_type] "r" (reset_type),
[cic_seed] "r" (cic_seed),
2023-09-04 23:31:19 +02:00
[version] "r" (version) :
"t3"
);
while (1);
}