If what you are trying to do is be clever with arguments so you don't have consider all possible arguments every time, you could do something like this:
Language: lua
registers = {C=2, V=3};
function set_flags(CSPR, flagPairs)
for k, v in pairs(flagPairs) do
local mask = bit.lshift(1,registers[k]);
CSPR = bit.bor(bit.band(CSPR, bit.bnot(mask)), bit.band(bit.lshift(v, registers[k]), mask));
end
return CSPR;
end
while true do
local CSPR = 8;
t = {C=1,V=0};
print(set_flags(CSPR, t));
emu.frameadvance();
end
This would output "4".
Ignoring the bit hacking fuckery, the main gist here is that you set up a table with the names of your registers and their associated position in your integer. Then when you want to set or clear specific bits, you pass in a table with keys that are the register names and either a 0 or a 1 depending on what you're doing. This way you only pass in pairs of registers and values that you want to modify.
I literally only tested this to see that it compiles and the sample output is correct, so make sure to test yourself.
EDIT:
Code inside the for loop that is actually human-readable would look like:
Language: lua
if v then
CSPR = bit.set(CSPR, registers[k]);
else
CSPR = bit.clear(CSPR, registers[k]);
end
and you would pass in bools instead of integer 1s and 0s.