-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepillepsy_ender.lua
131 lines (106 loc) · 3.92 KB
/
epillepsy_ender.lua
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
--[[
This may sound like a noble goal, but in reality, I wrote this beause I had a headache ._.
This is a bit screwy. I more or less abandoned this and used a ROM hack for the video I ultimately published:
https://www.youtube.com/watch?v=jKhm1tkOT5g
This version attempts to apply epilepsy reduction to all 16 BG colors.
]]
--[[
https://stackoverflow.com/questions/41954718/how-to-get-ppu-memory-from-fceux-in-lua
Oh hey, there's an answer from me there now!
]]
function memory.writebyteppu(a,v)
--memory.writebyte(0x2001,0x00) -- Turn off rendering
memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)
memory.writebyte(0x2006,math.floor(a/0x100)) -- PPUADDR high byte
memory.writebyte(0x2006,a % 0x100) -- PPUADDR low byte
memory.writebyte(0x2007,v) -- PPUDATA
--memory.writebyte(0x2001,0x1e) -- Turn on rendering
end
function memory.readbyteppu(a)
--memory.writebyte(0x2001,0x00) -- Turn off rendering
memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)
memory.writebyte(0x2006,math.floor(a/0x100)) -- PPUADDR high byte
memory.writebyte(0x2006,a % 0x100) -- PPUADDR low byte
if a < 0x3f00 then
dummy=memory.readbyte(0x2007) -- PPUDATA (discard contents of internal buffer if not reading palette area)
end
ret=memory.readbyte(0x2007) -- PPUDATA
--memory.writebyte(0x2001,0x1e) -- Turn on rendering
return ret
end
function memory.readbytesppu(a,l)
--memory.writebyte(0x2001,0x00) -- Turn off rendering
local ret
local i
ret=""
for i=0,l-1 do
memory.readbyte(0x2002) -- PPUSTATUS (reset address latch)
memory.writebyte(0x2006,math.floor((a+i)/0x100)) -- PPUADDR high byte
memory.writebyte(0x2006,(a+i) % 0x100) -- PPUADDR low byte
if (a+i) < 0x3f00 then
dummy=memory.readbyte(0x2007) -- PPUDATA (discard contents of internal buffer if not reading palette area)
end
ret=ret..string.char(memory.readbyte(0x2007)) -- PPUDATA
end
--memory.writebyte(0x2001,0x1e) -- Turn on rendering
return ret
end
local EPILEPSY_THRESHOLD = 9
local shouldOverwrite = {} -- all false
local prevBg = {} -- all an index into the palette
local replacementBg = {} -- all 0
local count = {} -- all 0
local softPPUMask = 0
local prevSoftPPUMask = 0
for i=0,0xF do
prevBg[i] = memory.readbyteppu(0x3F00+i)
replacementBg[i] = 0
count[i] = 0
end
-- we're going to need to apply a similar watch to all BG colors
local function overwriteBG(addr, size, val)
update()
for i=0,0xF do
if shouldOverwrite[i] then
memory.writebyteppu(0x3F00+i, replacementBg[i])
end
end
end
--memory.registerwrite(0x2007, update) -- PPUDATA writes
--memory.registerexec(0xCFED, update) -- Beginning of NMI
--memory.registerexec(0xD0D3, update) -- End of NMI
--memory.registerexec(0xD0FF, update) -- Beginning of draw routine
memory.registerexec(0xD117, overwriteBG) -- end of draw routine
function update()
--print(string.format("soft ppu mask: %02X", memory.readbyte(0xF8)))
--print(string.format("ANDED: %02X", AND(memory.readbyte(0xF8), 0x1E)))
if AND(softPPUMask, 0x1E)~=0x1E then
print("setting all shouldOverwrite to flase")
for i=0,0xF do
shouldOverwrite[i] = false
count[i] = EPILEPSY_THRESHOLD+1
prevBg[i] = memory.readbyteppu(0x3F00+i)
end
return
end
for i=0,0xF do
local bg = memory.readbyteppu(0x3F00+i)
if bg==prevBg[i] then
count[i] = count[i] + 1
else
count[i] = 1
shouldOverwrite[i] = true
end
prevBg[i] = bg
if count[i]==EPILEPSY_THRESHOLD then replacementBg[i] = bg end
if count[i] >= EPILEPSY_THRESHOLD then shouldOverwrite[i] = false end
end
end
--emu.registerafter(main)
function postFrame()
prevSoftPPUMask = softPPUMask
softPPUMask = memory.readbyte(0xF8)
--print(string.format("soft ppu mask post frame: %02X", softPPUMask))
--print(string.format("prev soft ppu mask post frame: %02X", prevSoftPPUMask))
end
emu.registerafter(postFrame)