JSFiddle - React, Tailwind, and code Playground

by devdev

HTML

<button onclick="program_setup('not')">NOT gate</button>
<button onclick="program_setup('or')">OR gate</button>
<div id="log"></div>

JavaScript

function log_write(text){
	document.all.log.innerHTML+=text
}

function io_read_bit_from_input_port(){
  html="in: <span>" 
  + "<button onclick='read_bit(this, 0)'>0</button>"
  + "<button onclick='read_bit(this, 1)'>1</button>"
  + " (select input!)  </span>"
	log_write(html+"<br>")
}

read_bit = function(source, value){
  bit_data=value
  source.parentElement.innerHTML=value
  data_input = true
  timeout=setTimeout(step,0)
}

function io_write_bit_to_output_port(bit_data){
	log_write("out: "+bit_data+"<br>")
}

program_not=[

["DESTINATION_PATH_SELECTOR_BIT", "ORIGIN_INPUT_PORT", 1,2],

["DESTINATION_OUTPUT_PORT", "ORIGIN_CONSTANT_ONE", "INSTRUCTION_EXIT","INSTRUCTION_EXIT"],

["DESTINATION_OUTPUT_PORT", "ORIGIN_CONSTANT_ZERO", "INSTRUCTION_EXIT","INSTRUCTION_EXIT"],

]

program_or=[
["DESTINATION_PATH_SELECTOR_BIT", "ORIGIN_INPUT_PORT", 1,2],
["DESTINATION_PATH_SELECTOR_BIT", "ORIGIN_INPUT_PORT", 3,4],
["DESTINATION_PATH_SELECTOR_BIT", "ORIGIN_INPUT_PORT", 4,4],


["DESTINATION_OUTPUT_PORT", "ORIGIN_CONSTANT_ZERO", "INSTRUCTION_EXIT","INSTRUCTION_EXIT"],

["DESTINATION_OUTPUT_PORT", "ORIGIN_CONSTANT_ONE", "INSTRUCTION_EXIT","INSTRUCTION_EXIT"],

]

program_setup=function(program_name){
	document.all.log.innerHTML="";
	program=window["program_"+program_name]
  current_instruction_index=0
	data_input=false
  timeout=setTimeout(step,0)
}

// execution loop
function step(){

// fetch current intruction
current_instruction = program[current_instruction_index]
console.log(current_instruction)

// ---------- bit-copy operation -----------

// 2nd field is for copy-origin
field_origin = current_instruction[1]

if( data_input ) { data_input = false } else
if( field_origin == "ORIGIN_INPUT_PORT" ) {

	// read 1 bit from the input port
	io_read_bit_from_input_port()
	return

}else if( field_origin == "ORIGIN_CONSTANT_ZERO" ) {

	// constant 0
	bit_data = 0

} else if( field_origin == "ORIGIN_CONSTANT_ONE" ){

	// constant 1
	bit_data = 1
	
} else {

	// read data from...