User File #27905811443158947

Upload All User Files

#27905811443158947 (unlisted) - Pokemon Plays Twitch input script

pptinput.lua
588 downloads
Uploaded 12/31/2015 5:28 PM by Ilari (49 files)
Very hacky script to input codeword sequences from file into PPT payload.
load_register = {0, 0, 0, 0};
do_load = true;
file = nil;
phase = 0;
new_frame_starting = true;
frame_words = {}
frame_phase = 7;
have_read_frame = false;
polling_began = 0;
no_idle_looping = true;

for i=0,31 do frame_words[i] = 0; end

charmap = "\nabcdefghijklmnopqrstuvwxyz ?!:.".."\"#$%&\\'()*+,-./0123456789;,=@ABCDEFGHIJKLMNOP"..
		"QRSTUVWXYZ[]^_`{|}~";

getchar = function(x)
	if x == 0 then return "<LF>"; end
	if x == 127 then return "<idle>"; end
	if (x + 1) <= #charmap then
		return string.sub(charmap, x + 1, x + 1);
	else
		return string.format("<%x>", x);
	end
end

getcode = function(x)
	--x = bit.all(bit.lshift(x, 8) + bit.lrshift(x, 8), 0xFFFF);
	if x == 0xFFFF then return "" end
	if x < 0x8000 then
		return string.format("%s%s%s", getchar(bit.all(bit.lrshift(x, 10), 0x1F)),
			getchar(bit.all(bit.lrshift(x, 5), 0x1F)), getchar(bit.all(bit.lrshift(x, 0), 0x1F)));
	elseif bit.all(x, 0xFF) == 0 then
		return string.format("%s", getchar(bit.all(bit.lrshift(x, 8), 0x7F)));
	else
		return string.format("%s(red:%s)", getchar(bit.all(bit.lrshift(x, 8), 0x7F)), getchar(bit.all(x, 0xFF)));
	end
end


--If this is new frame, 

load_values=function()
	if not do_load then return end
	--print("load_values...");
	frame_phase = (frame_phase + 1) % 8;
	loaded = 1;
	load_register[1] = 0xFFFF;
	load_register[2] = 0xFFFF;
	load_register[3] = 0xFFFF;
	load_register[4] = 0xFFFF;
	while loaded < 5 do
		local rawline = file:read("*l");
		--If we didn't get anything, treat it as 0xFFFF without looping.
		if not rawline then
			break;
		end
		load_register[loaded] = tonumber("0x"..(rawline or "FFFF"));
		if no_idle_looping or load_register[loaded] ~= 0xFFFF then
			loaded = loaded + 1;
			new_frame_starting = false;
		elseif not new_frame_starting then
			break;
		end
	end
	if load_register[1] ~= 0 or load_register[2] ~= 0 or load_register[3] ~= 0 or load_register[4] ~= 0 then
		print(string.format("Loaded values: %04x %04x %04x %04x (cycle %u) [%s%s%s%s]", load_register[1],
			load_register[2], load_register[3], load_register[4], frame_phase, getcode(load_register[1]), getcode(load_register[2]), getcode(load_register[3]), getcode(load_register[4])));
	end
	local frame_phase2 = frame_phase;
	if frame_phase == 0 then frame_phase2 = 8; end
	for i=0,3 do frame_words[4*frame_phase2+i] = load_register[i+1] or 0xDEADBEEF; end
	do_load = false;
end

on_latch=function()
	--print(string.format("On latch, frame=%u", movie.currentframe()));
	--load_values()
	do_load = true;
end

on_snoop=function(a, b, c, d)
	if a == 1 and b == 0 and c == 0 then polling_began = movie.currentframe(); end
	if movie.currentframe() ~= polling_began and a > 0 then
		print(string.format("WARNING: Split subframe: began %u, now %u (%u/%u)!", polling_began,
			movie.currentframe(), a, b));
	end
	polling_began = movie.currentframe();
	--If last poll, reload values.
	if a == 2 and b == 1 and c == 15 then
		--Load the saved last frame.
		if frame_phase == 1 then for i=0,3 do frame_words[i] = frame_words[32 + i]; end end
		do_load=true;
		load_values();
	end
end

bits_swap = function(x)
	if x < 8 then return 7 - x else return 23 - x end
end

set_controller=function(p, c, x)
	for i=0,15 do
		local state = bit.test_any(x, bit.lshift(1, i)) and 1 or 0;
		--print(string.format("Set controller %u, %u, %u, %u", p, c, bits_swap(i), state));
		input.set2(p, c, bits_swap(i), state);
	end
end

on_frame=function()
	print("--- NEW FRAME ---");
	if have_read_frame then
		for i=0,31 do 
			consoleword = memory2.WRAM:iword(0x100 + 2 * i);
			if frame_words[i] ~= consoleword then
				print(string.format("Error transferring word %u: %04x != %04x", i, frame_words[i],
					consoleword));
			end
		end
	end
	new_frame_starting = true;
	--load_values();
end

on_input=function()
	have_read_frame = true;
	set_controller(1, 0, load_register[1]);
	set_controller(2, 0, load_register[2]);
	set_controller(1, 1, load_register[3]);
	set_controller(2, 1, load_register[4]);
end

file = io.open("wordsinput.dat");
load_values()