From 658bcc6ad854f4cd187a4d3b2847690f94666286 Mon Sep 17 00:00:00 2001 From: Redo Date: Fri, 4 Nov 2022 16:29:11 -0600 Subject: [PATCH] add gate/net pointer lists --- sim/compile.lua | 105 ------------------------------------------- sim/compiled_sim.c | 74 ------------------------------ sim/compiled_sim.dll | Bin 39180 -> 38492 bytes sim/gate.lua | 23 ++++++++++ sim/gatedef.lua | 1 - sim/group.lua | 47 +++++++++++++++---- sim/network.lua | 2 +- 7 files changed, 62 insertions(+), 190 deletions(-) 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; ih~zNIU>jI*LE?RIrUu|9UhQJJFaW5m^fD}#Vq7#6#zWdA2O$?dggCb{4J zo$q|F^PQX9_7Cx&9OVag7cJl8adQri`-tbbFej8wBYX*0z;T>r{}VNQerW&4WyG}q zhjKDT&#u08zc!gJAPX`M=0ih)PXXOS=8!`A4^mD?NFM!$=(0`VoC_bA$W{P9QS2aBocXJktR90$Kib= zS;OISVRH`sB1O$Ra%hH7JNq_)TB_Wsu_>PT%?r9ykJLz8*w!-3w*#d{ZNR4jo8hqn)rUU)N$zMpELJ?VEDd(;kZ z?{|@4>CQ93%t*l>qxY9c*EQ%X3Xt%fC_?Y+g==5aow>yKy|!;5*;}x)M?HKq{qt;P z`*1JO-LWaK(dTJ#ZrQkLUW?B+tSdT6hE#>gX${li8lT}(O*koq^_PpL zRMdrX^=kd?`jvXGzEj_?|C4@5e^P&5|D8V5P+%xF%rPuAI1F12yA8iJJZ?B_c*^js z;d#R=hPMo-4IdjW8CKP_)a`j=8NWW^L2BQCEa4Q*et6p`z?c(zgV8Jj95-szOW=&)2%sHz13`Ou|8rw zW<6m&Y5m;#m36{;)0$g%TU~XXsqRwUwL0FGZp*g0ZGKy~ZI^AIjoRL~owYf>v0b+% z*>mkm`%HV4-EH^Te`SBve%OA@e%$`5{iK~!ahx8d&ey87I&H1is_oT2qb<`_>gMar zx;u1Bbjx&&y7js*=U<(FV?JM9UA?24{$_S7PV<3E1wW=5&a1phgfW%FdqXumy{cWA z&)TTatmL?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F6iWvb%hfa(iTp=sdTFB02wKknlJAP=Is-d&-x&XbOH9=gU0Sg2>&Q z2f_=tA!cU@NRKQRP8PjAPuT)KjB{Xnn%9RW|Cy2E-UY%V;3h;f`$4!-DW*3-`k^Jo zeFmgaX6GxA&!HvB{0hR0WM`r`j*wk4Ls=lh{5*l6IJ^<$13XEFmVvlsgVlnZ2q)K3 zThqut9K0STeAp()jL7VC#Cdr~`8%|ft`?B1vXuHjc#-^lY#ccNqLSHpYn;rfAW=ow8CnFymya~b*MdTki+Unn zkuZG`YEwaSWrJnKGyM*AeV=R!^B~64BhAiIkbi?n(Kdth%e3wT32h2-Nj)}3LX?nw z?8O~g@0;MfNO}1Lq&mFR9Ic*~rrM3}-7@>%j?+@&tq=*1@G27oM@NL`MIf_eA>ZKz z8j`8GK_C<1PzMMvl8aA*Tm_L*aCl5cBA6e*)8bZ8C!yf?Y1CfUR z7f3pYWcu7VtxF*CSX1y)%qO3(LV-AzH9DQ{;#~H@g9*zG=;9M9cA-HEK;%n$F-Vsz z!U~XMkd++sfMkdB@&GeDG*r&Nn&4~6xKAErtRwhsMf{8+BeKsX*Z1^J?0 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