csaboka

Untitled

Jun 24th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. function get_name()
  2.     return "DISK DRIVE CONTROLLER"
  3. end
  4.  
  5. NUM_TRACKS = 8
  6. NUM_SECTORS = 8
  7.  
  8. function get_description()
  9.     return {
  10.         "*input* is a non-blocking XBus input connected to a computer.",
  11.         "*output* is an XBus output connected to the same computer.",
  12.         "*motor* is a simple output connected to a stepper motor.",
  13.         "*read* is a simple input connected to the read head.",
  14.         "<PAGE>",
  15.         "The purpose of this device is to read data from a rotating data disk.",
  16.         "The disk is divided into " .. NUM_TRACKS .. " tracks and " .. NUM_SECTORS .. " sectors.",
  17.         "(For a visual representation, refer to the 'Disk sector' article on Wikipedia.)",
  18.         "The outermost track is track zero, then numbering increases inwards.",
  19.         "The read 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.",
  20.         "The disk is rotating at the fixed speed of one sector per time unit.",
  21.         "<PAGE>",
  22.         "Input consists of disk coordinates <track>,<sector>.",
  23.         "Move the head to the specified track via *motor*, wait until the correct sector gets rotated below the head, then send the value of *read* to *output*",
  24.         "The head starts at track zero. Rotation starts at sector zero."
  25.     }
  26. end
  27.  
  28. function get_board()
  29.     return [[
  30.         #.##############.#
  31.         ##################
  32.         0################2
  33.         ##################
  34.         1################3
  35.         ##################
  36.         #.##############.#
  37.     ]]
  38. end
  39.  
  40. function get_data()
  41.     math.random()   -- this is just to force nicer values for test case #1
  42.     local disk_contents = {}
  43.     for i=0,NUM_TRACKS-1 do
  44.         disk_contents[i] = {}
  45.         for j=0,NUM_SECTORS-1 do
  46.             disk_contents[i][j] = math.random(1, 100)
  47.         end
  48.     end
  49.    
  50.     local current_track = 0
  51.     local next_input_time = math.random(1, 2)
  52.     local target_track = 0
  53.     local target_sector = -1
  54.     local input = {}
  55.     local output = {}
  56.     local motor = {}
  57.     local read_arr = {}
  58.     for time = 1,60 do
  59.         local current_sector = (time-1)%NUM_SECTORS
  60.         read_arr[time] = disk_contents[current_track][current_sector]
  61.         if time == next_input_time then
  62.             target_track = math.random(0, NUM_TRACKS-1)
  63.             target_sector = math.random(0, NUM_SECTORS-1)
  64.             input[time] = {target_track, target_sector}
  65.         end
  66.         if current_track < target_track then
  67.             motor[time] = 100
  68.         elseif current_track > target_track then
  69.             motor[time] = 0
  70.         else
  71.             motor[time] = 50
  72.         end
  73.         if current_track == target_track and current_sector == target_sector then
  74.             output[time] = {read_arr[time]}
  75.             target_sector = -1
  76.             next_input_time = math.max(time + math.random(1, 2), next_input_time+2)
  77.         end
  78.         if motor[time] == 100 then
  79.             current_track = current_track + 1
  80.         elseif motor[time] == 0 then
  81.             current_track = current_track - 1
  82.         end
  83.     end
  84.    
  85.     create_terminal('input', '0', TYPE_XBUS_NONBLOCKING, DIR_INPUT, input)
  86.     create_terminal('output', '1', TYPE_XBUS, DIR_OUTPUT, output)
  87.     create_terminal('motor', '2', TYPE_SIMPLE, DIR_OUTPUT, motor)
  88.     create_terminal('read', '3', TYPE_SIMPLE, DIR_INPUT, read_arr)
  89. end
Advertisement
Add Comment
Please, Sign In to add comment