Skip to content

Commit b1820bc

Browse files
committed
wip: integer division approximation
Signed-off-by: Aleksey Gurtovoy <aleks@wildbitscomputing.com>
1 parent b48dbed commit b1820bc

4 files changed

Lines changed: 114 additions & 148 deletions

File tree

source/common/aa.system/04data.inc

Lines changed: 63 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
1-
; ************************************************************************************************
2-
; ************************************************************************************************
3-
;
4-
; Name: 04data.inc
5-
; Purpose: Main Data
6-
; Created: 18th September 2022
7-
; Reviewed: 23rd November 2022
8-
; Author: Paul Robson (paul@robsons.org.uk)
9-
;
10-
; ************************************************************************************************
11-
; ************************************************************************************************
121

13-
; ************************************************************************************************
14-
;
15-
; Mandatory Zero page code
16-
;
17-
; ************************************************************************************************
2+
; Mandatory zero-page data
183

194
.section zeropage
205

216
codePtr: ; address of current line (allow for 32 bit)
227
.fill 4
238
basicStack: ; BASIC stack address
24-
.fill 2
9+
.fill 2
2510
zTemp0: ; temporary words used in the interpreter.
2611
.fill 2
2712
zTemp1:
@@ -32,61 +17,68 @@ zsTemp: ; allocated string temporary.
3217
.fill 2
3318
zaTemp: ; allocated array temporary
3419
.fill 2
35-
20+
3621
safePtr = codePtr ; minimise direct tinkering
37-
38-
.send zeropage
3922

40-
; ************************************************************************************************
41-
;
42-
; Preferable Zero page code
43-
;
44-
; ************************************************************************************************
23+
.send zeropage
4524

46-
NSBIsNegative = $80 ; bit 7 : sign of mantissa (where numeric)
47-
NSBIsReference = $20 ; bit 5 : is a reference
48-
NSBIsString = $10 ; bit 4 : set if string (procedures don't go on the stack)
49-
NSBTypeMask = $18 ; bits 4,3 : 00 int 01 float 10 string 11 procedure
50-
NSBRefMask = $03 ; bits 0,1 : Bytes of reference, 00 = 4,01 = 1,10=2
51-
NSBIsArray = $04
5225

53-
NSTInteger = $00 ; base types for bits 3..4
54-
NSTFloat = $08
55-
NSTString = $10
56-
NSTProcedure = $18
26+
; Preferably zero-page data
5727

5828
.section zeropref
5929

60-
; ************************************************************************************************
61-
;
62-
; The number stack (works up from zero)
63-
;
64-
; ************************************************************************************************
30+
; Number stack status masks
6531

32+
NSBIsNegative = $80 ; bit 7: sign of mantissa (where numeric)
33+
NSBIsReference = $20 ; bit 5: is a reference
34+
NSBIsString = $10 ; bit 4: set if string (procedures don't go on the stack)
35+
NSBTypeMask = $18 ; bits 4,3: 00 int 01 float 10 string 11 procedure
36+
NSBRefMask = $03 ; bits 0,1: Bytes of reference, 00 = 4,01 = 1,10=2
37+
NSBIsArray = $04
38+
39+
NSTInteger = $00 ; base types for bits 3..4
40+
NSTFloat = $08
41+
NSTString = $10
42+
NSTProcedure = $18
43+
44+
;;
45+
; The number stack (works up from zero)
46+
;;
6647
NSStatus: ; Status bits of stack.
6748
.fill MathStackSize
68-
NSMantissa0: ; Mantissa on stack (address in 0,1 for string/reference)
69-
.fill MathStackSize ; (this is my integer mantissa system)
70-
NSMantissa1: ; the order of the mantissa elements is required.
49+
50+
; 4 mantissa bytes (little-endian, 31 bits + sign)
51+
;
52+
; Stores an integer magnitude. Normalized non-zero mantissa always has bit 30 set.
53+
;
54+
; Note that on the number stack, the sign is kept in `NSStatus` (`NSBIsNegative` mask), so the
55+
; mantissa itself only carries magnitude; when a float is written out to a variable, the sign
56+
; bit gets packed into the top bit of the highest mantissa byte.
57+
NSMantissa0:
58+
.fill MathStackSize
59+
NSMantissa1:
7160
.fill MathStackSize
7261
NSMantissa2:
7362
.fill MathStackSize
7463
NSMantissa3:
7564
.fill MathStackSize
76-
NSExponent: ; Exponent , 0 = Mantissa is integer
65+
66+
67+
; a signed 8-bit base-2 exponent stored in two's complement format; 0 if mantissa is integer
68+
;
69+
; floating-point value = mantissa x 2^decoded exponent, where
70+
; decoded exponent = exponent if exponent < 128 else exponent - 256
71+
NSExponent:
7772
.fill MathStackSize
7873

79-
fnStackLevel: ; saved stack level for function evaluation
74+
; saved stack level for function evaluation
75+
fnStackLevel:
8076
.fill 1
8177

8278
.send zeropref
8379

8480

85-
; ************************************************************************************************
86-
;
87-
; Non Zero Page Data
88-
;
89-
; ************************************************************************************************
81+
; Non-zero page data
9082

9183
.section storage
9284

@@ -95,7 +87,7 @@ identStart: ; start of identifier in line buffer
9587
identTypeStart: ; start of type information (#$ and ( in the line buffer)
9688
.fill 1
9789
identTypeEnd: ; character after end of type information in the line buffer
98-
.fill 1
90+
.fill 1
9991
identHash: ; hash of identifier (including the type characters)
10092
.fill 1
10193
identTypeByte: ; type descriptor in format used in identifier record
@@ -113,63 +105,63 @@ lowMemPtr: ; memory allocation after program.
113105
arrayMemPtr: ; array/alloc storage pointer (slots 2-3, $4000-$7FFF)
114106
.fill 2
115107
stringMemory: ; allocate concrete strings from here
116-
.fill 2
117-
stringInitialised: ; string
118-
.fill 1
108+
.fill 2
109+
stringInitialised: ; string
110+
.fill 1
119111
stringTempPointer: ; temporary string
120-
.fill 2
112+
.fill 2
121113

122114
breakCheck: ; break check counter/shift
123115
.fill 1
124-
116+
125117
decimalPlaces: ; no of decimals.
126118
.fill 1
127119
dbOffset: ; offset into decimal buffer.
128-
.fill 1
120+
.fill 1
129121

130122
lastParameter: ; final stack offset when all parameters handled.
131123
.fill 1
132-
124+
133125
dataPointer: ; operates like code pointer for DATA statements
134126
.fill 5
135127
inDataStatement: ; non zero when in data statement, e.g. pointing to data
136-
.fill 1
128+
.fill 1
137129

138130
tbOffset: ; offset into token buffer
139-
.fill 1
131+
.fill 1
140132

141133
AssemblerAddress: ; address being assembled at
142134
.fill 2
143135
AssemblerControl: ; assembler control byte - bit 0 (errors ok), bit 1 (listing on)
144-
.fill 1
136+
.fill 1
145137
ParamStart: ; offset of parameter.
146-
.fill 2
138+
.fill 2
147139
IsGroup1: ; flag set if group.1
148-
.fill 1
140+
.fill 1
149141
BaseOpcode: ; base opcode
150-
.fill 1
142+
.fill 1
151143
ModeMask: ; modes allowed
152-
.fill 1
144+
.fill 1
153145
listIndent: ; list indent level.
154146
.fill 1
155147
listElseFound: ; flag set when ELSE found on line (for indent fix)
156148
.fill 1
157149
listEndToken: ; closing token for LIST proc/fn
158150
.fill 1
159151
lcLastCharacter: ; last character output.
160-
.fill 1
152+
.fill 1
161153
isPrintFlag: ; zero if input, non-zero if print, bit 6 zero = character print
162-
.fill 1
154+
.fill 1
163155
currentListColour: ; current set colour when listing.
164156
.fill 1
165157
;
166158
; New tokenised line - these three *must* be contiguous as they are used as a single entity
167-
;
159+
;
168160
tokenOffset: ; used for tokenising - tokenised result goes here
169-
.fill 1
161+
.fill 1
170162
tokenLineNumber:
171-
.fill 2
172-
tokenBuffer:
163+
.fill 2
164+
tokenBuffer:
173165
.fill 253
174166

175167
lineBuffer: ; used for input - ASCII line goes here
@@ -193,19 +185,3 @@ decimalBuffer: ; buffer for number -> string conversion.
193185
.fill 24
194186

195187
.send storage
196-
197-
; ************************************************************************************************
198-
;
199-
; Changes and Updates
200-
;
201-
; ************************************************************************************************
202-
;
203-
; Date Notes
204-
; ==== =====
205-
; 1/12/2022 Changed the recorded values used in the reference byte size which were
206-
; incorrect, no code change.
207-
; 2/12/2022 Made line buffer long again, as long lines can be entered via cross
208-
; development which would crash the interpreter.
209-
;
210-
; ************************************************************************************************
211-
Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,46 @@
1-
; ************************************************************************************************
2-
; ************************************************************************************************
3-
;
4-
; Name: integer.asm
5-
; Purpose: Make FPA Denormalised integer
6-
; Created: 29th September 2022
7-
; Reviewed: 28th November 2022
8-
; Author : Paul Robson (paul@robsons.org.uk)
9-
;
10-
; ************************************************************************************************
11-
; ************************************************************************************************
121

132
.section code
143

15-
; ************************************************************************************************
4+
;;
5+
; Convert floating-point entry at position X in the number stack to an
6+
; integer.
167
;
17-
; Make FPA into an integer
18-
;
19-
; ************************************************************************************************
20-
8+
; Sets carry if the value was not already an exact integer.
9+
;;
2110
FloatIntegerPart:
2211
pha
23-
;
12+
phy
13+
ldy #$0 ; store the carry status in Y
14+
2415
lda NSExponent,x ; is it integer already ?
25-
beq _FIPExit ; if so do nothing
16+
beq _exit ; if so do nothing
2617
jsr NSMIsZero ; is it zero ?
27-
beq _FIPZero ; if so return zero.
28-
;
18+
beq _zero ; if so return zero.
19+
2920
jsr NSNormalise ; normalise
30-
beq _FIPZero ; normalised to zero, exit zero
31-
;
32-
_FIPShift:
21+
beq _zero ; normalised to zero, exit zero
22+
23+
_shift:
3324
lda NSExponent,x ; if Exponent >= 0 exit.
34-
bpl _FIPCheckZero
25+
bpl _check_zero
3526

3627
jsr NSMShiftRight ; shift mantissa right
37-
inc NSExponent,x ; bump exponent
38-
bra _FIPShift
28+
bcc +
29+
ldy #$1 ; record that we lost a non-zero bit
30+
+ inc NSExponent,x ; bump exponent
31+
bra _shift
3932

40-
_FIPCheckZero:
33+
_check_zero:
4134
jsr NSMIsZero ; avoid -0 problem
42-
bne _FIPExit ; set to zero if mantissa zero.
43-
_FIPZero:
35+
bne _exit ; set to zero if mantissa zero.
36+
37+
_zero:
4438
jsr NSMSetZero
45-
_FIPExit:
39+
40+
_exit:
41+
cpy #$1 ; sets the carry flag if Y is 1
42+
ply
4643
pla
47-
rts
44+
rts
4845

4946
.send code
50-
51-
; ************************************************************************************************
52-
;
53-
; Changes and Updates
54-
;
55-
; ************************************************************************************************
56-
;
57-
; Date Notes
58-
; ==== =====
59-
;
60-
; ************************************************************************************************

source/common/expressions/float/utility.asm

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@ FloatPrepare:
2828
_FDType:
2929
jmp TypeError
3030

31-
; ************************************************************************************************
32-
;
33-
; Normalise Stack[X]
31+
32+
;;
33+
; Normalize floating-point entry at position X in the number stack.
3434
;
35-
; ************************************************************************************************
35+
; \in X Index of the entry in the number stack.
36+
;;
3637

3738
NSNormalise:
3839
lda NSStatus,x ; make float, keep sign
3940
and #$80
40-
ora #NSTFloat
41+
ora #NSTFloat
4142
sta NSStatus,x
4243

43-
jsr NSMIsZero ; if zero exit
44+
jsr NSMIsZero ; if zero exit
4445
bne _NSNormaliseOptimise ; if so, normalise it.
4546
asl NSStatus,x ; clear the sign bit.
4647
ror NSStatus,x ; (no -0)
@@ -50,7 +51,7 @@ NSNormalise:
5051
; Normalise by byte if the MSB is zero we can normalise it
5152
; (providing bit 7 of 2nd byte is not set)
5253
;
53-
_NSNormaliseOptimise:
54+
_NSNormaliseOptimise:
5455
lda NSMantissa3,x ; upper byte zero ?
5556
bne _NSNormaliseLoop
5657
lda NSMantissa2,x ; byte normalise
@@ -71,7 +72,7 @@ _NSNormaliseOptimise:
7172
;
7273
; Normalise by bit
7374
;
74-
_NSNormaliseLoop:
75+
_NSNormaliseLoop:
7576
bit NSMantissa3,x ; bit 30 set ?
7677
bvs _NSNExit ; exit if so with Z flag clear
7778
jsr NSMShiftLeft ; shift mantissa left
@@ -82,7 +83,7 @@ _NSNExit:
8283
rts
8384

8485
.send code
85-
86+
8687
; ************************************************************************************************
8788
;
8889
; Changes and Updates

0 commit comments

Comments
 (0)