Math device

This is the user manual for the math device of the Bedrock computer system.

This document is aimed at people who are learning about or writing programs for the Bedrock system. For people who are implementing the system from scratch, the math device specification will provide more relevant information.

Overview

The math device provides access to 16-bit multiplication and division operations, and conversions between two-dimensional points in the cartesian and polar coordinate systems.

Concepts

The first half of this device is used for converting between the cartesian and polar coordinate systems.

The cartesian coordinate system marks a point using an x coordinate and a y coordinate: the x coordinate measures distance from the origin along a horizontal axis, and the y coordinate measures distance from the origin along a vertical axis. Coordinates are treated as signed doubles, where the values 0000 to 7FFF represent 0 to +32767, and the values FFFF to 8000 represent -1 to -32768.

The polar coordinate system marks a point using an r coordinate and a t coordinate: the r coordinate measures distance from the origin (called the radius), and the t coordinate measures the angle anti-clockwise around the origin from the positive horizontal axis (called theta, or \theta). The angle unit is 1/65536 of a full turn, which means that the full range of a double marks a complete circle:

t coord Degrees Compass
0000 E
2000 45° NE
4000 90° N
6000 135° NW
8000 180° W
A000 225° SW
C000 270° S
E000 315° SE

Writing to the x and y coordinate ports will mark the position of a point in the cartesian coordinate space, and writing to the r and t coordinate ports will mark the position of a separate point in the polar coordinate space. Reading back from these ports will then perform the conversions, with the x and y coordinate ports returning the cartesian coordinates of the polar point, and the r and t coordinate ports returning the polar coordinates of the cartesian point.

The second half of this device is used for performing multiplication and division operations. The unsigned x coordinate and y coordinate values are used as the operands.

Ports

Port Name Description Read Write
20 x coordinate Horizontal coordinate.
21 grouped grouped
22 y coordinate Vertical coordinate.
23 grouped grouped
24 r coordinate Radial coordinate.
25 grouped grouped
26 t coordinate Angular coordinate.
27 grouped grouped
28 Product Product of multiplication.
29 grouped grouped
2A grouped grouped
2B grouped grouped
2C Quotient Quotient of division.
2D grouped grouped
2E Remainder Remainder of division.
2F grouped grouped

x coordinate

Writing a double to this port will set the x coordinate of the cartesian point. Reading a double from this port will return the x coordinate of the polar point after conversion, or zero if the result is too large.

See the multiplication and division and the coordinate conversion examples.

y coordinate

Writing a double to this port will set the y coordinate of the cartesian point. Reading a double from this port will return the y coordinate of the polar point after conversion, or zero if the result is too large.

See the multiplication and division and the coordinate conversion examples.

r coordinate

Writing a double to this port will set the r coordinate of the polar point. Reading a double from this port will return the r coordinate of the cartesian point after conversion.

See the coordinate conversion example.

t coordinate

Writing a double to this port will set the t coordinate of the polar point. Reading a double from this port will return the t coordinate of the cartesian point after conversion.

See the coordinate conversion example.

Product

This port uses the values written to the x coordinate and y coordinate ports as unsigned doubles, called the x operand and the y operand. Reading from this port will multiply the x operand by the y operand and return the result.

See the multiplication and division example.

Quotient

This port uses the values written to the x coordinate and y coordinate ports as unsigned doubles, called the x operand and the y operand. Reading from this port will divide the x operand by the y operand and return the result. If the y operand is zero, the result will be zero.

See the multiplication and division example.

Remainder

This port uses the values written to the x coordinate and y coordinate ports as unsigned doubles, called the x operand and the y operand. Reading from this port will divide the x operand by the y operand and return the remainder. If the y operand is zero, the remainder will be zero.

See the multiplication and division example.

Wake behaviour

This device will never wake the system from sleep.

Examples

Multiplication and division

The product, quotient, and remainder ports use the values written to the x coordinate and y coordinate ports as unsigned doubles, called the x operand and the y operand:

*:C201 STD*:20  (           ) ( set the x operand  )
*:0002 STD*:22  (           ) ( set the y operand  )
LDD*:2C         ( 6100      ) ( read the quotient  )
LDD*:2E         ( 6100 0001 ) ( read the remainder )

The product port group is four ports long, so the full product can only be obtained with two double-width reads:

*:0102 STD*:20  (           ) ( set the x operand    )
*:0200 STD*:22  (           ) ( set the y operand    )
LDD*:28         ( 0002      ) ( read the high double )
LDD*:2A         ( 0002 0400 ) ( read the low double  )

Coordinate conversion

The cartesian point is set by writing to the x coordinate and y coordinate ports, and is then converted to the polar coordinate space by reading from the r coordinate and t coordinate ports:

*:1000 STD*:20  (           ) ( set x coordinate            )
*:1000 STD*:22  (           ) ( set y coordinate            )

LDD*:24         ( 16A0      ) ( load converted r coordinate )
LDD*:26         ( 16A0 2000 ) ( load converted t coordinate )

The polar point can be converted to the cartesian coordinate space by reversing the operations. Be aware of rounding errors when converting repeatedly between the two systems:

*:16A1 STD*:24  (           ) ( set r coordinate            )
*:2000 STD*:26  (           ) ( set t coordinate            )

LDD*:20         ( 1000      ) ( load converted x coordinate )
LDD*:22         ( 1000 1000 ) ( load converted y coordinate )