Tutorial T1 – Registers

This tutorial introduces the concept of registers.

// Tutorial 1 - Registers

// TUTORIALS ARE DESIGNED TO BE READ AND STEPPED THROUGH IN THE
// APRON SIMULATOR. THEY WILL NOT DO INTERESTING THINGS IF RAN!
// (c) David R W Barr 2011

// This tutorial shows how to construct 2D arrays, which in APRON
// are considered registers. Registers should have a multiple of 4
// elements in them. For example, an 8x8 register has 64 elements, 
// which is valid. A 7x10 register is not evenly divisible by 4, and
// consequently many operations may produce unusual artifacts. Why?
// APRON uses SSE for almost all computation, which performs operations
// on 4 elements in parallel.

// However, it is OK to use odd-sized registers if you do not perform
// array operations upon them. 

// For the best performance, it is recommended that registers with
// dimensions that are a power of 2 are used.


!include('apron.aps')

// Create a register labelled 'a' with dimensions 128x64. The type REGISTER
// is already defined by the APRON subsystem. The "create" function creates
// an object of type REGISTER called "a", which is 128 elements wide by 64
// elements high.
!create('a', REGISTER, 128, 64)

// We can use the "!define" preprocessor command to create some constants
// which make it easier to scale the algorithm
!define(WIDTH, 128)
!define(HEIGHT, 64)

// Create some more registers
!create('b', REGISTER, WIDTH, HEIGHT)
!create('c', REGISTER, WIDTH, HEIGHT)
!create('d', REGISTER, WIDTH, HEIGHT)
!create('e', REGISTER, WIDTH, HEIGHT)
!create('f', REGISTER, WIDTH, HEIGHT)


// All elements in a register are operated upon simultaneously. An object of type
// REGISTER can be accessed with the prefix "r".

r[a] = 35 // Set entire register to the value 35

r[b] = 55

// Basic numeric operators have already been defined in "apron.aps".
r[c] = r[a] + r[b]
r[d] = r[a] * r[b]
r[e] = r[a] / r[b]
r[f] = r[a] - r[b]

// Note that the default palette in APRON is configured to show -128 -> 0 as red, and
// 0 -> +128 as green. Palettes can be assigned algorithmically and individually. The 
// global palette can be set using the "!default_palette()" preprocessor function.

// Palettes must exist in the APRON palette database. You can see this by hitting CTRL+4 in
// Crimson Editor.

r[a] = reg.setPalette('Raw', 0) // 0 -> 255 greyscale. The '0' is currently unused.

// Often it is convenient to configure the APRON Simulator via the algorithm, instead
// of manually each time you change something.

sim.display(r[a]) // Sends the register r[a] to the workspace (same as clicking on thumbnail)
sim.display(r[b])
sim.display(r[c])

sim.tile // Positions registers (same as clicking on tile button)


// Registers are mainly used to store spatial data, such as images. APRON is fast
// enough to perform many operations on video data. However, for performance reasons,
// when the simulation is "RUN" it is too slow to update the contents of all registers
// on screen. When in "RUN" mode we can use a special call to only update a selected register.

// HIT "RUN" NOW!
counter = 0
#loop
	r[a] = counter
	counter = counter + 1
	if(counter, ==, 255)
		counter = 0
	endif
	sim.show(r[a]) // Force an update in r[a] when running
	jump(#loop)


end

This entry was posted in Documentation, Examples. Bookmark the permalink.

Leave a Reply