remove gate ffi struct
This commit is contained in:
		
							
								
								
									
										25
									
								
								sim/gate.lua
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								sim/gate.lua
									
									
									
									
									
								
							| @@ -3,21 +3,11 @@ local ffi = FFI or require("ffi") | |||||||
|  |  | ||||||
| Gate = {} | Gate = {} | ||||||
|  |  | ||||||
| ffi.cdef [[ |  | ||||||
| 	struct OutPort { |  | ||||||
| 		struct Net* net; |  | ||||||
| 		int state; |  | ||||||
| 	}; |  | ||||||
| 	struct Gate { |  | ||||||
| 		int in_queue; |  | ||||||
| 		struct OutPort ports[0]; |  | ||||||
| 	}; |  | ||||||
| ]] |  | ||||||
|  |  | ||||||
| function Gate.new(objref, definition) | function Gate.new(objref, definition) | ||||||
| 	local gate = { | 	local gate = { | ||||||
| 		-- Logic Critical | 		-- Logic Critical | ||||||
| 		c = nil, | 		in_queue = ffi.new("int"), | ||||||
|  | 		port_states = ffi.new("int["..(#definition.ports+1).."]"), | ||||||
| 		logic = definition.logic, | 		logic = definition.logic, | ||||||
| 		ports = {}, | 		ports = {}, | ||||||
| 		port_nets = {}, | 		port_nets = {}, | ||||||
| @@ -25,23 +15,20 @@ function Gate.new(objref, definition) | |||||||
| 		objref = objref, | 		objref = objref, | ||||||
| 		definition = definition, | 		definition = definition, | ||||||
| 	} | 	} | ||||||
| 	local cdata = ffi.new("char["..(ffi.sizeof("struct Gate") + ffi.sizeof("struct OutPort")*(#definition.ports+1)).."]") |  | ||||||
| 	gate.c = ffi.cast("struct Gate*", cdata) |  | ||||||
| 	gate.c.in_queue = 0 |  | ||||||
| 	return gate | 	return gate | ||||||
| end | end | ||||||
|  |  | ||||||
| -- Logic Critical | -- Logic Critical | ||||||
| function Gate.getportstate(gate, index) | function Gate.getportstate(gate, index) | ||||||
| 	return gate.c.ports[index].state | 	return gate.port_states[index] | ||||||
| end | end | ||||||
|  |  | ||||||
| -- Logic Critical | -- Logic Critical | ||||||
| function Gate.setportstate(gate, index, state) | function Gate.setportstate(gate, index, state) | ||||||
| 	if state ~= gate.c.ports[index].state then | 	if state ~= gate.port_states[index] then | ||||||
| 		local group = gate.port_nets[index] | 		local group = gate.port_nets[index] | ||||||
| 		group.state_num = group.state_num - gate.c.ports[index].state + state | 		group.state_num = group.state_num - gate.port_states[index] + state | ||||||
| 		gate.c.ports[index].state = state | 		gate.ports_states[index] = state | ||||||
| 		 | 		 | ||||||
| 		if ((group.state_num>0) ~= (group.state==1)) and (group.in_queue==0) then | 		if ((group.state_num>0) ~= (group.state==1)) and (group.in_queue==0) then | ||||||
| 			Simulation.queuegroup(GSim, group) | 			Simulation.queuegroup(GSim, group) | ||||||
|   | |||||||
| @@ -75,6 +75,7 @@ end | |||||||
| function GateDefinition.constructgate(def, objref, position, rotation) | function GateDefinition.constructgate(def, objref, position, rotation) | ||||||
| 	local gate = Gate.new(objref, def) | 	local gate = Gate.new(objref, def) | ||||||
| 	 | 	 | ||||||
|  | 	gate.in_queue = 0 | ||||||
| 	for i = 1, #def.ports do | 	for i = 1, #def.ports do | ||||||
| 		local portd = def.ports[i] | 		local portd = def.ports[i] | ||||||
| 		local type = portd.type | 		local type = portd.type | ||||||
| @@ -102,7 +103,7 @@ function GateDefinition.constructgate(def, objref, position, rotation) | |||||||
| 		 | 		 | ||||||
| 		gate.ports[port.idx] = port | 		gate.ports[port.idx] = port | ||||||
| 		gate.port_nets[port.idx] = nil | 		gate.port_nets[port.idx] = nil | ||||||
| 		gate.c.ports[port.idx].state = 0 | 		gate.ports_states[port.idx] = 0 | ||||||
| 	end | 	end | ||||||
| 	 | 	 | ||||||
| 	return gate | 	return gate | ||||||
|   | |||||||
| @@ -195,7 +195,7 @@ function Group.setstate(group, state) | |||||||
| 		local len = group.num_gates_update | 		local len = group.num_gates_update | ||||||
| 		for i = 1, len do | 		for i = 1, len do | ||||||
| 			local gate = group.gates_update[i] | 			local gate = group.gates_update[i] | ||||||
| 			if gate and gate.c.in_queue==0 then | 			if gate and gate.in_queue==0 then | ||||||
| 				Simulation.queuegate(sim, gate) | 				Simulation.queuegate(sim, gate) | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
|   | |||||||
| @@ -62,5 +62,5 @@ function Port.gettype(port) | |||||||
| end | end | ||||||
|  |  | ||||||
| function Port.getstate(port) | function Port.getstate(port) | ||||||
| 	return Port.getgate(port).c.ports[port.idx].state | 	return Port.getgate(port).port_states[port.idx] | ||||||
| end | end | ||||||
|   | |||||||
| @@ -244,11 +244,11 @@ end | |||||||
| function Simulation.queuegate(sim, gate) | function Simulation.queuegate(sim, gate) | ||||||
| 	sim.gatequeue[sim.num_gatequeue+1] = gate | 	sim.gatequeue[sim.num_gatequeue+1] = gate | ||||||
| 	sim.num_gatequeue = sim.num_gatequeue + 1 | 	sim.num_gatequeue = sim.num_gatequeue + 1 | ||||||
| 	gate.c.in_queue = 1 | 	gate.in_queue = 1 | ||||||
| end | end | ||||||
|  |  | ||||||
| function Simulation.queuegate_safe(sim, gate) | function Simulation.queuegate_safe(sim, gate) | ||||||
| 	if gate.c.in_queue==0 then | 	if gate.in_queue==0 then | ||||||
| 		Simulation.queuegate(sim, gate) | 		Simulation.queuegate(sim, gate) | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
| @@ -296,10 +296,10 @@ function Simulation.dequeuegroup(sim, group) | |||||||
| end | end | ||||||
|  |  | ||||||
| function Simulation.dequeuegate(sim, gate) | function Simulation.dequeuegate(sim, gate) | ||||||
| 	if gate.c.in_queue~=0 then | 	if gate.in_queue~=0 then | ||||||
| 		array_remove(sim.gatequeue, gate, true) | 		array_remove(sim.gatequeue, gate, true) | ||||||
| 		sim.num_gatequeue = sim.num_gatequeue - 1 | 		sim.num_gatequeue = sim.num_gatequeue - 1 | ||||||
| 		gate.c.in_queue = 0 | 		gate.in_queue = 0 | ||||||
| 	end | 	end | ||||||
| 	if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end | 	if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end | ||||||
| 	if sim.initqueue ~=nil then sim.initqueue [gate] = nil end | 	if sim.initqueue ~=nil then sim.initqueue [gate] = nil end | ||||||
| @@ -330,7 +330,7 @@ function Simulation.ticklogic(sim) | |||||||
| 	 | 	 | ||||||
| 	if sim.tickqueue[sim.current_tick] ~= nil then | 	if sim.tickqueue[sim.current_tick] ~= nil then | ||||||
| 		for i, gate in pairs(sim.tickqueue[sim.current_tick]) do | 		for i, gate in pairs(sim.tickqueue[sim.current_tick]) do | ||||||
| 			if gate.c.in_queue==0 then | 			if gate.in_queue==0 then | ||||||
| 				Simulation.queuegate(sim, gate) | 				Simulation.queuegate(sim, gate) | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| @@ -340,7 +340,7 @@ function Simulation.ticklogic(sim) | |||||||
| 	for i = 1, sim.num_gatequeue do | 	for i = 1, sim.num_gatequeue do | ||||||
| 		local gate = sim.gatequeue[i] | 		local gate = sim.gatequeue[i] | ||||||
| 		gate.logic(gate) | 		gate.logic(gate) | ||||||
| 		gate.c.in_queue = 0 | 		gate.in_queue = 0 | ||||||
| 		sim.gatequeue[i] = nil | 		sim.gatequeue[i] = nil | ||||||
| 	end | 	end | ||||||
| 	--sim.gatequeue = {} | 	--sim.gatequeue = {} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Redo
					Redo