Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_name()
- return "ADVANCED DISK DRIVE CONTROLLER"
- end
- NUM_TRACKS = 8
- NUM_SECTORS = 8
- function get_description()
- return {
- "*input* is a non-blocking XBus input connected to a computer.",
- "*output* is an XBus output connected to the same computer.",
- "*motor* is a simple output connected to a stepper motor.",
- "*read* is a simple input connected to the read/write head.",
- "*write* is a simple output connected to the read/write head.",
- "<PAGE>",
- "The purpose of this device is to read data from, and write data to, a rotating data disk.",
- "The disk is divided into " .. NUM_TRACKS .. " tracks and " .. NUM_SECTORS .. " sectors.",
- "(For a visual representation, refer to the 'Disk sector' article on Wikipedia.)",
- "The outermost track is track zero, then numbering increases inwards.",
- "The read/write head of the drive can be moved between tracks via *motor*. 50 makes the head stay still, 0 moves it one track outwards, 100 moves it one track inwards. The head will complete the requested movement by the start of the *next* time unit.",
- "The disk is rotating at the fixed speed of one sector per time unit.",
- "<PAGE>",
- "Input consists of data packets <track>,<sector>,<data>.",
- "Move the head to the specified track via *motor*, then wait until the correct sector gets rotated below the head.",
- "Then, if <data> is -1, send the value of *read* to *output*.",
- "Otherwise, send <data> to *write* to write the new value to the disk.",
- "*write* must be kept zero when not writing, to avoid overwriting unrelated data.",
- "The head starts at track zero. Rotation starts at sector zero."
- }
- end
- function get_board()
- return [[
- #.##############.#
- #################2
- 0#################
- #################3
- 1#################
- #################4
- #.##############.#
- ]]
- end
- function get_data()
- math.random() -- this is just to force nicer values for test case #1
- local disk_contents = {}
- for i=0,NUM_TRACKS-1 do
- disk_contents[i] = {}
- for j=0,NUM_SECTORS-1 do
- disk_contents[i][j] = math.random(1, 100)
- end
- end
- local current_track = 0
- local next_input_time = math.random(1, 2)
- local target_track = 0
- local target_sector = -1
- local target_value = 0
- local input = {}
- local output = {}
- local motor = {}
- local read_arr = {}
- local write_arr = {}
- for time = 1,60 do
- local current_sector = (time-1)%NUM_SECTORS
- read_arr[time] = disk_contents[current_track][current_sector]
- if time == next_input_time then
- target_track = math.random(0, NUM_TRACKS-1)
- target_sector = math.random(0, NUM_SECTORS-1)
- if math.random(0, 1) == 0 then
- target_value = -1
- else
- target_value = math.random(1, 100)
- end
- input[time] = {target_track, target_sector, target_value}
- end
- if current_track < target_track then
- motor[time] = 100
- elseif current_track > target_track then
- motor[time] = 0
- else
- motor[time] = 50
- end
- write_arr[time] = 0
- if current_track == target_track and current_sector == target_sector then
- if target_value < 0 then
- output[time] = {read_arr[time]}
- else
- write_arr[time] = target_value
- disk_contents[target_track][target_sector] = target_value
- end
- target_sector = -1
- next_input_time = math.max(time + math.random(1, 2), next_input_time+3)
- end
- if motor[time] == 100 then
- current_track = current_track + 1
- elseif motor[time] == 0 then
- current_track = current_track - 1
- end
- end
- create_terminal('input', '0', TYPE_XBUS_NONBLOCKING, DIR_INPUT, input)
- create_terminal('output', '1', TYPE_XBUS, DIR_OUTPUT, output)
- create_terminal('motor', '2', TYPE_SIMPLE, DIR_OUTPUT, motor)
- create_terminal('read', '3', TYPE_SIMPLE, DIR_INPUT, read_arr)
- create_terminal('write', '4', TYPE_SIMPLE, DIR_OUTPUT, write_arr)
- end
Advertisement
Add Comment
Please, Sign In to add comment