diff --git a/sim/compile.lua b/sim/compile.lua index c8adce4..8b13789 100644 --- a/sim/compile.lua +++ b/sim/compile.lua @@ -1,106 +1 @@ -local ffi = FFI or require("ffi") -Simulation = Simulation or {} - -function Simulation.compile_code(sim, text) - -- todo: compile some kind of DSL into machine code - - return code, size -end - ---local net_program_code = Simulation.compile_code( [[ --- ---]] ) - -function Simulation.compile(sim) - sim.compilation = { - gates = {}, - wires = {}, - cgates = {}, - cwires = {}, - } - local comp = sim.compilation - - -- assemble a list of all nets - local all_nets_t = {} - for k, wire in pairs(sim.wires) do - local net = Wire.getgroup(wire) - all_nets_t[net] = net - end - local all_nets = {} - for net_id, net in pairs(all_nets_t) do - table.insert(comp.nets, net) - local cdata = ffi.new("char["..(ffi.sizeof("struct Net") + ffi.sizeof("struct Gate*")*net.num_gates_update).."]") - local cnet = ffi.cast(cdata, "struct Net") - comp.cnets[net] = cnet - end - - -- assemble a list of all gates - local all_gates = {} - for k, gate in pairs(sim.gates) do - table.insert(comp.gates, gate) - local cdata = ffi.new("char["..(ffi.sizeof("struct Gate") + ffi.sizeof("struct OutPort")*gate.num_ports_out).."]") - local cgate = ffi.cast(cdata, "struct Gate") - comp.cgates[gate] = cgate - end - - for netidx, net in ipairs(comp.nets) do - local cnet = comp.cnets[net] or error("no cnet") - cnet.in_queue = net.in_queue - cnet.num_out_ports_on = net.state_num - cnet.state = net.state - cnet.update_tick = net.update_tick - for i = 1, net.num_gates_update do - local gate = net.gates_update[i] - local cgate = comp.cgates[gate] or error("no cgate") - cnet.gates_update[i-1] = cgate - end - end - - for gateidx, gate in ipairs(comp.gates) do - local cgate = comp.cgates[gate] or error("no cgate") - cgate.in_queue = gate.in_queue - local j = 0 - for i, port in ipairs(gate.ports) do - if port.type == PortTypes.output then - local net = port.group - if net then - local cnet = comp.cnets[net] or error("no cnet") - cgate.out_ports[j].net = cnet - else - cgate.out_ports[j].net = 0 - end - cgate.out_ports[j].state = gate.port_states[i] or error("no gate port_state") - j = j + 1 - end - end - end -end - -function Simulation.decompile(sim) - local comp = sim.compilation - - for netidx, net in ipairs(comp.nets) do - local cnet = comp.cnets[net] or error("no cnet") - net.in_queue = cnet.in_queue - net.state_num = cnet.num_out_ports_on - net.state = cnet.state - net.update_tick = cnet.update_tick - end - - for gateidx, gate in ipairs(comp.gates) do - local cgate = comp.cgates[gate] or error("no cgate") - gate.in_queue = cgate.in_queue - local j = 0 - for i, port in ipairs(gate.ports) do - if port.type == PortTypes.output then - gate.port_states[i] = cgate.out_ports[j].state - j = j + 1 - end - end - end -end - -function Simulation.tick_compiled(sim, count) - -end diff --git a/sim/compiled_sim.c b/sim/compiled_sim.c index da809c4..8b13789 100644 --- a/sim/compiled_sim.c +++ b/sim/compiled_sim.c @@ -1,75 +1 @@ -void sim_init(int num_gates, int num_nets); -void sim_add_gate(int gateid, int logic_func_id, char* logic_func_name, int num_out_ports, char* data); -void sim_add_net(int netid, int num_gates_update); -void sim_connect_out_port(int gateid, int index, int netid); -void sim_connect_in_port(int gateid, int index, int netid); -void sim_tick(int count); -void sim_get_net_state(int netid); -void sim_get_port_state(int gateid, int index); -void sim_set_net_state(int netid); -void sim_set_port_state(int gateid, int index); -void sim_delete_net(int netid); -void sim_delete_gate(int gateid); - -//////// - -void sim_create(unsigned int datalen, char* text); - -#include -#include - -typedef unsigned int uint; -typedef unsigned long long u64; - -void sim_copy_logic_function( - char** prog, - char* in_queue, - char* out_port_states, - int logic_func, - char** port_in_net_states, - char** port_out_net_inqueue, - char** port_out_net_update_func, - int num_ports_in, - int num_ports_out -) { - // todo: indata = inport net state pointers, outport net in queue pointers, outport net update function pointers -} - -void sim_compile_gate(char** prog, char** data, int** idat, u64* indata) { - int num_ports_in = *(indata++); - int num_ports_out = *(indata++); - int in_queue = *(indata++); - int logic_func = *(indata++); - - char* port_in_net_states[256]; - sim_copy_logic_function( // copy logic function into program - prog, - *data, - (*data)+1, - logic_func, - &indata, - num_ports_in, - num_ports_out - ); - - *(*data++) = in_queue; // alloc bool for in queue - for(int i=0; i0 and 1 or 0) end -function Group.rebuild_ports(group) - group.gates_update = {} - group.num_gates_update = 0 - for k, port in pairs(group.in_ports) do +function Group.rebuild_ports(net) + net.gates_update = {} + net.num_gates_update[0] = 0 + local gates_seen = {} + for k, port in pairs(net.in_ports) do if port.causeupdate then - array_add(group.gates_update, Port.getgate(port)) - group.num_gates_update = group.num_gates_update + 1 + local gate = Port.getgate(port) + if not gates_seen[gate] then + gates_seen[gate] = true + net.gates_update[net.num_gates_update[0]+1] = gate + net.num_gates_update[0] = net.num_gates_update[0] + 1 + end end end + net.gates_update_c = ffi.new("struct Gate*["..(net.num_gates_update[0]).."]") + for i = 0, net.num_gates_update[0]-1 do + net.gates_update_c[i] = net.gates_update[i+1].c + end end diff --git a/sim/network.lua b/sim/network.lua index a2352cd..73c9127 100644 --- a/sim/network.lua +++ b/sim/network.lua @@ -141,7 +141,7 @@ function network_update() local group = Wire.getgroup(wire) local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end local numportsi = 0; for k, port in pairs(group.in_ports ) do numportsi = numportsi+1 end - local numgatesu = group.num_gates_update + local numgatesu = group.num_gates_update[0] local numportso = 0; local numportson=0; for k, port in pairs(group.out_ports) do numportso = numportso+1