You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package intcode
  2. // VirtualMachine is an IntCode virtual machine.
  3. type VirtualMachine struct {
  4. ip int
  5. rb int
  6. parameterModes uint8
  7. relativeModes uint8
  8. Memory []int
  9. Halted bool
  10. Input chan int
  11. Output chan int
  12. }
  13. // NewVirtualMachine creates a new IntCode virtual machine, initialised to the given slice of memory.
  14. // The caller is responsible for initialising the VM's I/O channels if required.
  15. func NewVirtualMachine(memory []int) *VirtualMachine {
  16. vm := &VirtualMachine{
  17. ip: 0,
  18. Memory: memory,
  19. Halted: false,
  20. }
  21. return vm
  22. }
  23. // arg Returns the value of the given argument for the current instruction.
  24. func (vm *VirtualMachine) arg(pos int) *int {
  25. mask := uint8(1) << uint8(pos)
  26. if vm.parameterModes&mask == mask {
  27. // Parameter mode - the value of the argument is just treated as an int
  28. for vm.ip+1+pos > len(vm.Memory) {
  29. vm.Memory = append(vm.Memory, make([]int, 1024)...)
  30. }
  31. return &vm.Memory[vm.ip+1+pos]
  32. } else if vm.relativeModes&mask == mask {
  33. // Relative mode - the value of the argument is treated as a memory offset from the relative base
  34. for vm.ip+1+pos > len(vm.Memory) || vm.rb+vm.Memory[vm.ip+1+pos] > len(vm.Memory) {
  35. vm.Memory = append(vm.Memory, make([]int, 1024)...)
  36. }
  37. return &vm.Memory[vm.rb+vm.Memory[vm.ip+1+pos]]
  38. } else {
  39. // Position mode - the value of the argument is treated as a memory offset from the start of the memory
  40. for vm.ip+1+pos > len(vm.Memory) || vm.Memory[vm.ip+1+pos] > len(vm.Memory) {
  41. vm.Memory = append(vm.Memory, make([]int, 1024)...)
  42. }
  43. return &vm.Memory[vm.Memory[vm.ip+1+pos]]
  44. }
  45. }
  46. // Run repeatedly executes instructions until the VM halts.
  47. func (vm *VirtualMachine) Run() {
  48. for !vm.Halted {
  49. instruction := vm.Memory[vm.ip]
  50. opcode := instruction % 100
  51. vm.parameterModes = 0
  52. vm.relativeModes = 0
  53. mask := uint8(1)
  54. for i := instruction / 100; i > 0; i /= 10 {
  55. mode := i % 10
  56. if mode == 1 {
  57. vm.parameterModes = vm.parameterModes | mask
  58. } else if mode == 2 {
  59. vm.relativeModes = vm.relativeModes | mask
  60. }
  61. mask = mask << 1
  62. }
  63. opcodes[opcode](vm)
  64. }
  65. if vm.Output != nil {
  66. close(vm.Output)
  67. }
  68. }
  69. // Reset resets the memory to the given slice, and all other state back to its original value.
  70. func (vm *VirtualMachine) Reset(memory []int) {
  71. copy(vm.Memory, memory)
  72. // We may previously have expanded our own memory, reset that to zero.
  73. for i := len(memory); i < len(vm.Memory)-1; i++ {
  74. vm.Memory[i] = 0
  75. }
  76. vm.ip = 0
  77. vm.rb = 0
  78. vm.Halted = false
  79. }