::::::::::::::::::: :: CORDIC demo 8 :: ::::::::::::::::::: 8b education intro for ZX Spectrum 16k Code: Busy soft Create: 25.01.2025 Release: LoveByte 2025 This intro shows simplified CORDIC algorithm intended for computing size of two-dimensed vector. Now some math theory follows :) Pythagorean Theorem =================== It provides this computation, based od Pythagorean Theorem: Size = SQR(X^2+Y^2) Where X and Y are vector coordinates. For more info about Pythagoras, look here: https://en.wikipedia.org/wiki/Pythagorean_theorem CORDIC in general ================= The CORDIC itself is based on rotating the vector. It starts with a vector whose X coordinate is positive whereas the Y coordinate is arbitrary. Successive rotations have the goal of rotating the vector to the X axis and therefore reducing the Y coordinate to zero. When the Y is zero, the X coordinate is size of the vector. For more info about generic CORDIC, look here: https://en.wikipedia.org/wiki/CORDIC Bresenham's circle drawing algorithm ==================================== For the rotating of vector, the reverse bresenham's circle drawing algorithm is used. Originally, it is used for successive rotations the initial vector [X,0] - with X coordinate (it is radius) on the X asix and Y zero, to the another vector [new_x,new_y) with the same size, but in our case, we reverse the direction of rotating and the goal is to get the vector to the X axis. For more info about bresenham's circle algorithm, look here: https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/ Conclusion of theory ==================== It is possible to imagine our work by this way: Let's have a circle with unknown radius and middle at [0,0]. Let's have a point [X,Y] what is situated anywhere on the circle. Now we move this point on the circle until its Y coordinate is not zero. When Y coordinate of the point is zero, the X coordinate is equal to the radius of the circle and it is our wanted size of vector [X,Y]. Implementation ============== Input: C = coordinate X B = coordinate Y Output: C = result = SQR(X^2+Y^2) ld a,c L1: sub b jr nc,L2 inc c add c L2: djnz L1 Register C contains X coordinate, what are increased by rotating vector or moving point on the circle. Register B is counter of iterations or successive rotations, it is initialized to coordinate Y. Register A holds bresenham's iterator used for computation. At begin, it is initialized to coordinate X. To proper working of this simplified routine, a few mandatory conditions must be fulfilled: (X^2+Y^2) < 65536 ... Since result is 8 bit only, we must make sure to not produce overflow Y <= X .............. Number of iterations cannot be bigger than input coordinate X Y > 0 ............... Number of iterations in priciple can be zero (then result is directly X) but due to using DJNZ, zero means 256 iterations and it is not good