A program for today
ElEctric_EyE wrote:
It does convert without using decimal mode?...
BigEd wrote:
I notice, I think, that Bruce cut off the output just before the point where the algorithm needs an extra wrinkle. Which is only fair - he had a deadline!
BigEd wrote:
There's a (somewhat mathematical) paper here explaining the algorithm - which also gives us a clue as to how far we can get with 8-bit bytes, and how much further we might get with 16-bit bytes. The mul and div routines would need some 8-counts adjusting to 16, at least.
RichTW wrote:
And there was me thinking that it must use some previously unknown secret feature of the 6502 that made it start doing arithmetic in base pi
I did some experimenting (in awk, converted from the pascal in that paper), and it looks like a 65Org16 version should be able to operate in base 1000, getting 3 digits out per iteration, producing nearly 2000 digits before it runs out of headroom. (And no counting or buffering.)
I think a 16-bit version for 65816 would have to do just a little juggling to distinguish X the index from X the address: a double increment here, a shift there. The '816 has the compensating merit of having emulators and many real chips in the field!
I think a 16-bit version for 65816 would have to do just a little juggling to distinguish X the index from X the address: a double increment here, a shift there. The '816 has the compensating merit of having emulators and many real chips in the field!
I have been monkeying around with this some more. Here is a version that runs on the 65C816, intended to be easily adapted to the 65org16 -- just remove the JSRs to ADJ, UNADJ, ADJ1, and UNADJ1 (or replace those routines with an RTS) and remove the *2 from the DSs at the beginning. (There are more efficient ways to index the P array on the 65C816 than the ADJ routines, of course.)
It buffers, but stops before counting is necessary. Because a 16-bit intermediate result is sufficient, MUL, DIV, and the inner loop are simplified and the end result is a program that is about the same size as (an adapted version of) the original. (Some sequences were replaced with 65C02 instructions as well). It also speeds up as it goes.
I also tried changing LDY #8 to LDY #16 (with the ADJ routines and *2 in the DS) and that worked on a 65C816, so hopefully I haven't missed anything involved in porting to the 65org16. I also tried a buffering and counting version (with a 32-bit intermediate result), which produced over 9800 digits, as expected. The program itself was all that much lengthier (though probably lengthier than the slight amount of extra work needed to output base 1000 in assembly), but it took longer to complete (on a simulator) than I would have liked, even when speeding up as it goes.
It buffers, but stops before counting is necessary. Because a 16-bit intermediate result is sufficient, MUL, DIV, and the inner loop are simplified and the end result is a program that is about the same size as (an adapted version of) the original. (Some sequences were replaced with 65C02 instructions as well). It also speeds up as it goes.
I also tried changing LDY #8 to LDY #16 (with the ADJ routines and *2 in the DS) and that worked on a 65C816, so hopefully I haven't missed anything involved in porting to the 65org16. I also tried a buffering and counting version (with a 32-bit intermediate result), which produced over 9800 digits, as expected. The program itself was all that much lengthier (though probably lengthier than the slight amount of extra work needed to output base 1000 in assembly), but it took longer to complete (on a simulator) than I would have liked, even when speeding up as it goes.
Code: Select all
P DS 1193*2
Q DS 1*2
R DS 1*2
S DS 1*2
JSR INIT
LDX #359
LDY #1193
L1 PHY
PHA
PHX
STZ Q
TYA
TAX
L2 TXA
JSR MUL
STA S
LDA #10
STA Q
JSR ADJ1
LDA P-1,X
JSR UNADJ1
JSR MUL
CLC
ADC S
STA Q
TXA
ASL
DEC
JSR DIV
JSR ADJ1
STA P-1,X
JSR UNADJ1
DEX
BNE L2
LDA #10
JSR DIV
STA P
PLX
PLA
LDY Q
CPY #10
BCC L3
LDY #0
INC
L3 CMP #358
BCC L4
BNE L5
JSR OUTPUT
LDA #46
L4 JSR OUTPUT
L5 TYA
EOR #48
PLY
CPX #358
BCS L6
DEY
DEY
DEY
L6 DEX
BNE L1
JMP OUTPUT
INIT
LDA #2
LDX #1192
I1 JSR ADJ
STA P,X
JSR UNADJ
DEX
BPL I1
RTS
MUL
STA R
LDY #16
M1 ASL
ASL Q
BCC M2
CLC
ADC R
M2 DEY
BNE M1
RTS
DIV
STA R
LDY #16
LDA #0
ASL Q
D1 ROL
CMP R
BCC D2
SBC R
D2 ROL Q
DEY
BNE D1
RTS
ADJ
PHA
TXA
ASL
TAX
PLA
RTS
UNADJ
PHA
TXA
LSR
TAX
PLA
RTS
ADJ1
PHA
TXA
ASL
TAX
PLA
DEX
RTS
UNADJ1
PHA
TXA
LSR
TAX
PLA
INX
RTS
Wow - it certainly does speed up as it goes! Excellent!
I just got to the point where I can run a version of this on 65Org16 on py65. With normal python, in one minute it gets this far:
Whereas running with pypy, it gets that far in something under 3 seconds - too quick to time accurately - and then spins out lots of digits increasingly quickly. (Might be a bug[*] - it blows up just before 400th character:
...62019643 is the last I see, takes 40 seconds to get there.)
Here's my port of your program, in super-friendly loadable format:
[*] Edit - yes, there was at least one bug, I still had PHX and PLX in my program, which are not implemented. Now the code stops at 360 digits. It still prints no decimal point, which I don't yet understand.
I just got to the point where I can run a version of this on 65Org16 on py65. With normal python, in one minute it gets this far:
Code: Select all
$ PYTHONPATH=. python ./py65/monitor.py -m 65Org16
Py65 Monitor
PC AC XR YR SP NV---------BDIZC
65Org16: 00000000 0000 0000 0000 ffff 0000000000110000
.load bootrom.bin fe00
Wrote +512 bytes from $0000fe00 to $0000ffff
PC AC XR YR SP NV---------BDIZC
65Org16: 00000000 0000 0000 0000 ffff 0000000000110000
.goto fe00
Send 65Org16 code in variant Intel Hex format at 19200,n,8,1 ->
###########
Download Successful!
Jumping to location $0200
31415926535897
...62019643 is the last I see, takes 40 seconds to get there.)
Here's my port of your program, in super-friendly loadable format:
Code: Select all
;06020000004C0B5C000045
;200B5C0000200BD7000000A2016700A004A900850B5B0098004800A50B5B004800850B5B17
;200B6C00008A004800A50B5B00850B5B00A9000000850B5500A50B5B009800AA008A00201C
;200B7C000BE1000000850B5900A9000A00850B5500B5020200200BE10000001800650B5946
;200B8C0000850B55008A000A003800E9000100200BF100000095020200CA00D0FFDF00A9D8
;200B9C00000A00200BF100000085020300850B5B006800AA00A50B5B006800A40B5500C055
;200BAC00000A0090000400A000000069000000C901660090000700D0000800200C060000B1
;200BBC0000A9002E00200C06000000980049003000850B5B006800A800A50B5B00E00166B2
;200BCC0000B0000300880088008800CA00D0FF8F004C0C06000000A9000200A204A80095AA
;200BDC00020300CA0010FFFB006000850B5700A00010000A00060B550090000300180065A9
;200BEC000B57008800D0FFF5006000850B5700A0001000A9000000060B55002A00C50B57E4
;1A0BFC000090000200E50B5700260B55008800D0FFF400600085F0010060FF
Great - that's done it! (I should have been able to see that - I certainly looked.)
Here's the code, which demonstrates how 65Org16 behaves at source level exactly like a 16-bit 6502 - which it is! The only effort in porting your 65816 source was in replacing the 65c02 opcodes which we don't presently have. (Of course you'd done the interesting and difficult bit of making good use of 16-bit registers already.)
Here's the code, which demonstrates how 65Org16 behaves at source level exactly like a 16-bit 6502 - which it is! The only effort in porting your 65816 source was in replacing the 65c02 opcodes which we don't presently have. (Of course you'd done the interesting and difficult bit of making good use of 16-bit registers already.)
Code: Select all
; pi program ported from the 65816 version
; by Bruce Clark as seen on 6502.org
.listfile
.errfile
.hexfile
.listoff includes
.listoff stats
.listoff labels
.cpu T_32_M16
.assume BIT32=1032, BIT32R=3210
.include "i6502.a"
; I/O is memory-mapped in py65:
PUTC = $f001
GETC = $f005 ; blocking input
; our bootstrap ROM will only load to $0200
.ORG $200
JMP START
P DS 1193*2
Q DS 1*2
R DS 1*2
S DS 1*2
TMP DS 1
START
JSR INIT
LDX #359
LDY #1193
L1
; no PHY on 65Org16
STA TMP
TYA
PHA
LDA TMP
PHA
; no PHX on 65Org16
STA TMP
TXA
PHA
LDA TMP
; no STZ on 65Org16
STA TMP
LDA #0
STA Q
LDA TMP
TYA
TAX
L2 TXA
JSR MUL
STA S
LDA #10
STA Q
LDA P-1,X
JSR MUL
CLC
ADC S
STA Q
TXA
ASL
; no DEC on 65Org16
SEC
SBC #1
JSR DIV
STA P-1,X
DEX
BNE L2
LDA #10
JSR DIV
STA P
; no PLX on 65Org16
STA TMP
PLA
TAX
LDA TMP
PLA
LDY Q
CPY #10
BCC L3
LDY #0
; no INC on 65Org16
; carry is set
ADC #0
L3 CPX #358
BCC L4
BNE L5
JSR OUTPUT
LDA #46
L4 JSR OUTPUT
L5 TYA
EOR #48
; no PLY on 65Org16
STA TMP
PLA
TAY
LDA TMP
CPX #358
BCS L6
DEY
DEY
DEY
L6 DEX
BNE L1
JMP OUTPUT
INIT
LDA #2
LDX #1192
I1 STA P,X
DEX
BPL I1
RTS
MUL
STA R
LDY #16
M1 ASL
ASL Q
BCC M2
CLC
ADC R
M2 DEY
BNE M1
RTS
DIV
STA R
LDY #16
LDA #0
ASL Q
D1 ROL
CMP R
BCC D2
SBC R
D2 ROL Q
DEY
BNE D1
RTS
OUTPUT
STA PUTC
RTS
Code: Select all
;020000040000FA
;06020000004C0B5C000045
;200B5C0000200BD7000000A2016700A004A900850B5B0098004800A50B5B004800850B5B17
;200B6C00008A004800A50B5B00850B5B00A9000000850B5500A50B5B009800AA008A00201C
;200B7C000BE1000000850B5900A9000A00850B5500B5020200200BE10000001800650B5946
;200B8C0000850B55008A000A003800E9000100200BF100000095020200CA00D0FFDF00A9D8
;200B9C00000A00200BF100000085020300850B5B006800AA00A50B5B006800A40B5500C055
;200BAC00000A0090000400A000000069000000E001660090000700D0000800200C0600009A
;200BBC0000A9002E00200C06000000980049003000850B5B006800A800A50B5B00E00166B2
;200BCC0000B0000300880088008800CA00D0FF8F004C0C06000000A9000200A204A80095AA
;200BDC00020300CA0010FFFB006000850B5700A00010000A00060B550090000300180065A9
;200BEC000B57008800D0FFF5006000850B5700A0001000A9000000060B55002A00C50B57E4
;1A0BFC000090000200E50B5700260B55008800D0FFF400600085F0010060FF
;00000001FF
Re: A program for today
I probably should point out that the reason this thread was titled A Program For Today is because the date of the original post is relevant. I'll leave it as an exercise for the reader to figure out what the relevance actually is.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: A program for today
bump
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: A program for today
and another. This one needs as big an audience as it can get 
Re: A program for today
Here's a big spoiler (so make sure you read the whole thread and have a think first) and here's Bruce's source translated for Easy 6502, so you can run it here.
Re: A program for today
dclxvi wrote:
Just supply an OUTPUT routine, which outputs the character in the accumulator (like pretty much every other output routine ever).
Code: Select all
STA PORTRe: A program for today
Not to start a flame war, but of course the, ah, underlying concept here (no spoilers) is famously wrong, and you should all be ashamed of yourselves for giving it space in a forum where there are young and impressionable minds present. See here for pure truth and beauty on this subject.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: A program for today
scotws wrote:
Not to start a flame war, but of course the, ah, underlying concept here (no spoilers) is famously wrong, and you should all be ashamed of yourselves for giving it space in a forum where there are young and impressionable minds present. See here for pure truth and beauty on this subject.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: A program for today
scotws wrote:
Not to start a flame war, but of course the, ah, underlying concept here (no spoilers) is famously wrong, and you should all be ashamed of yourselves for giving it space in a forum where there are young and impressionable minds present. See here for pure truth and beauty on this subject.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
I'm completely swamped by Real Life (TM) at the moment, but will try to get the right version done ASAP - at least until June 28th, that is.
That other shapes might have a constant diameter seems more like a red herring to me since the definition is about circles. Maybe we could define pi as the ratio of a circle's area to the square of its radius. Nothing could go wrong there! (Oh wait, some squares also have the same area
Re: A program for today
I'm all for using metric over imperial, but τ is a step too far for me. Exhibit A.