csaboka

Untitled

Jun 24th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. function get_name()
  2.     return "ADVANCED 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/write head.",
  14.         "*write* is a simple output connected to the read/write head.",
  15.         "<PAGE>",
  16.         "The purpose of this device is to read data from, and write data to, a rotating data disk.",
  17.         "The disk is divided into " .. NUM_TRACKS .. " tracks and " .. NUM_SECTORS .. " sectors.",
  18.         "(For a visual representation, refer to the 'Disk sector' article on Wikipedia.)",
  19.         "The outermost track is track zero, then numbering increases inwards.",
  20.         "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.",
  21.         "The disk is rotating at the fixed speed of one sector per time unit.",
  22.         "<PAGE>",
  23.         "Input consists of data packets <track>,<sector>,<data>.",
  24.         "Move the head to the specified track via *motor*, then wait until the correct sector gets rotated below the head.",
  25.         "Then, if <data> is -1, send the value of *read* to *output*.",
  26.         "Otherwise, send <data> to *write* to write the new value to the disk.",
  27.         "*write* must be kept zero when not writing, to avoid overwriting unrelated data.",
  28.         "The head starts at track zero. Rotation starts at sector zero."
  29.     }
  30. end
  31.  
  32. function get_board()
  33.     return [[
  34.         #.##############.#
  35.         #################2
  36.         0#################
  37.         #################3
  38.         1#################
  39.         #################4
  40.         #.##############.#
  41.     ]]
  42. end
  43.  
  44. function get_data()
  45.     math.random()   -- this is just to force nicer values for test case #1
  46.     local disk_contents = {}
  47.     for i=0,NUM_TRACKS-1 do
  48.         disk_contents[i] = {}
  49.         for j=0,NUM_SECTORS-1 do
  50.             disk_contents[i][j] = math.random(1, 100)
  51.         end
  52.     end
  53.    
  54.     local current_track = 0
  55.     local next_input_time = math.random(1, 2)
  56.     local target_track = 0
  57.     local target_sector = -1
  58.     local target_value = 0
  59.     local input = {}
  60.     local output = {}
  61.     local motor = {}
  62.     local read_arr = {}
  63.     local write_arr = {}
  64.     for time = 1,60 do
  65.         local current_sector = (time-1)%NUM_SECTORS
  66.         read_arr[time] = disk_contents[current_track][current_sector]
  67.         if time == next_input_time then
  68.             target_track = math.random(0, NUM_TRACKS-1)
  69.             target_sector = math.random(0, NUM_SECTORS-1)
  70.             if math.random(0, 1) == 0 then
  71.                 target_value = -1
  72.             else
  73.                 target_value = math.random(1, 100)
  74.             end
  75.             input[time] = {target_track, target_sector, target_value}
  76.         end
  77.         if current_track < target_track then
  78.             motor[time] = 100
  79.         elseif current_track > target_track then
  80.             motor[time] = 0
  81.         else
  82.             motor[time] = 50
  83.         end
  84.         write_arr[time] = 0
  85.         if current_track == target_track and current_sector == target_sector then
  86.             if target_value < 0 then
  87.                 output[time] = {read_arr[time]}
  88.             else
  89.                 write_arr[time] = target_value
  90.                 disk_contents[target_track][target_sector] = target_value
  91.             end
  92.             target_sector = -1
  93.             next_input_time = math.max(time + math.random(1, 2), next_input_time+3)
  94.         end
  95.         if motor[time] == 100 then
  96.             current_track = current_track + 1
  97.         elseif motor[time] == 0 then
  98.             current_track = current_track - 1
  99.         end
  100.     end
  101.    
  102.     create_terminal('input', '0', TYPE_XBUS_NONBLOCKING, DIR_INPUT, input)
  103.     create_terminal('output', '1', TYPE_XBUS, DIR_OUTPUT, output)
  104.     create_terminal('motor', '2', TYPE_SIMPLE, DIR_OUTPUT, motor)
  105.     create_terminal('read', '3', TYPE_SIMPLE, DIR_INPUT, read_arr)
  106.     create_terminal('write', '4', TYPE_SIMPLE, DIR_OUTPUT, write_arr)
  107. end
Advertisement
Add Comment
Please, Sign In to add comment