-
Notifications
You must be signed in to change notification settings - Fork 100
/
sfc_addr4020.c
92 lines (88 loc) · 2.74 KB
/
sfc_addr4020.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "sfc_6502.h"
#include "sfc_cpu.h"
#include "sfc_famicom.h"
#include <assert.h>
#include <string.h>
/// <summary>
/// StepFC: 读取CPU地址数据4020
/// </summary>
/// <param name="address">The address.</param>
/// <param name="famicom">The famicom.</param>
/// <returns></returns>
extern inline uint8_t sfc_read_cpu_address4020(uint16_t address, sfc_famicom_t* famicom) {
uint8_t data = 0;
switch (address & (uint16_t)0x1f)
{
case 0x16:
// 手柄端口#1
data = (famicom->button_states+0)[famicom->button_index_1 & famicom->button_index_mask];
++famicom->button_index_1;
break;
case 0x17:
// 手柄端口#2
data = (famicom->button_states+8)[famicom->button_index_2 & famicom->button_index_mask];
++famicom->button_index_2;
break;
}
return data;
}
/// <summary>
/// StepFC: 获取DMA地址
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
static inline const uint8_t* sfc_get_dma_address(uint8_t data, const sfc_famicom_t* famicom) {
const uint16_t offset = ((uint16_t)(data & 0x07) << 8);
switch (data >> 5)
{
default:
case 1:
// PPU寄存器
assert(!"PPU REG!");
case 2:
// 扩展区
assert(!"TODO");
case 0:
// 系统内存
return famicom->main_memory + offset;
case 3:
// 存档 SRAM区
return famicom->save_memory + offset;
case 4: case 5: case 6: case 7:
// 高一位为1, [$8000, $10000) 程序PRG-ROM区
return famicom->prg_banks[data >> 5] + offset;
}
}
/// <summary>
/// StepFC: 写入CPU地址数据4020
/// </summary>
/// <param name="address">The address.</param>
/// <param name="data">The data.</param>
/// <param name="famicom">The famicom.</param>
extern inline void sfc_write_cpu_address4020(uint16_t address, uint8_t data, sfc_famicom_t* famicom) {
switch (address & (uint16_t)0x1f)
{
case 0x14:
// 精灵RAM直接储存器访问
if (famicom->ppu.oamaddr) {
uint8_t* dst = famicom->ppu.sprites;
const uint8_t len = famicom->ppu.oamaddr;
const uint8_t* src = sfc_get_dma_address(data, famicom);
// 需要换行
memcpy(dst, src + len, len);
memcpy(dst + len, src, 256 - len);
}
else memcpy(famicom->ppu.sprites, sfc_get_dma_address(data, famicom), 256);
famicom->cpu_cycle_count += 513;
famicom->cpu_cycle_count += famicom->cpu_cycle_count & 1;
break;
case 0x16:
// 手柄端口
famicom->button_index_mask = (data & 1) ? 0x0 : 0x7;
if (data & 1) {
famicom->button_index_1 = 0;
famicom->button_index_2 = 0;
}
break;
}
}