6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 5:21 pm

All times are UTC




Post new topic Reply to topic  [ 39 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Thu Jul 28, 2011 1:40 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
ElEctric_EyE wrote:
It does convert without using decimal mode?...


Yes, that's correct. Decimal mode is not used.

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!


Actually, I finished this quite a while ago. With a division routine that uses an 8-bit denominator, you can only get 7 more digits before the denominator (of the first call to DIV) exceeds 8 bits. I wrote a version that buffers the previous digit (with 8 bits, you don't reach the point where you need to count nines), and it's a little longer, but not much. (Counting nines is longer still, but not a lot.) Since there wasn't much difference between the two versions, I chose the shortest one to (hopefully) encourage as many people as possible to try it.

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.


A 65org16 version (which would have a 32-bit intermediate result -- a 24-bit is sufficient -- and "P" would be an array of 16-bit values rather than 8-bit values) with buffering and counting can generate over 250 times as much output as an 8-bit version (and over 300 times as much as the above). Or, as you suggested, you could just change the LDY #8 to LDY #16 in MUL and DIV and be done with it.

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


2.222... = pi in base pi/(pi-2) (=~ 2.752) not base pi. :P


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jul 28, 2011 4:59 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 14, 2011 8:53 pm 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
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.

Code:
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Aug 22, 2011 4:40 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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:
Code:
$ 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


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:

Code:
;06020000004C0B5C000045
;200B5C0000200BD7000000A2016700A004A900850B5B0098004800A50B5B004800850B5B17
;200B6C00008A004800A50B5B00850B5B00A9000000850B5500A50B5B009800AA008A00201C
;200B7C000BE1000000850B5900A9000A00850B5500B5020200200BE10000001800650B5946
;200B8C0000850B55008A000A003800E9000100200BF100000095020200CA00D0FFDF00A9D8
;200B9C00000A00200BF100000085020300850B5B006800AA00A50B5B006800A40B5500C055
;200BAC00000A0090000400A000000069000000C901660090000700D0000800200C060000B1
;200BBC0000A9002E00200C06000000980049003000850B5B006800A800A50B5B00E00166B2
;200BCC0000B0000300880088008800CA00D0FF8F004C0C06000000A9000200A204A80095AA
;200BDC00020300CA0010FFFB006000850B5700A00010000A00060B550090000300180065A9
;200BEC000B57008800D0FFF5006000850B5700A0001000A9000000060B55002A00C50B57E4
;1A0BFC000090000200E50B5700260B55008800D0FFF400600085F0010060FF


[*] 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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 24, 2011 2:09 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
BigEd wrote:
It still prints no decimal point, which I don't yet understand.


My bad. The CMP #358 at L3 should be CPX.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Aug 24, 2011 4:13 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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.)

Code:
; 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:
;020000040000FA
;06020000004C0B5C000045
;200B5C0000200BD7000000A2016700A004A900850B5B0098004800A50B5B004800850B5B17
;200B6C00008A004800A50B5B00850B5B00A9000000850B5500A50B5B009800AA008A00201C
;200B7C000BE1000000850B5900A9000A00850B5500B5020200200BE10000001800650B5946
;200B8C0000850B55008A000A003800E9000100200BF100000095020200CA00D0FFDF00A9D8
;200B9C00000A00200BF100000085020300850B5B006800AA00A50B5B006800A40B5500C055
;200BAC00000A0090000400A000000069000000E001660090000700D0000800200C0600009A
;200BBC0000A9002E00200C06000000980049003000850B5B006800A800A50B5B00E00166B2
;200BCC0000B0000300880088008800CA00D0FF8F004C0C06000000A9000200A204A80095AA
;200BDC00020300CA0010FFFB006000850B5700A00010000A00060B550090000300180065A9
;200BEC000B57008800D0FFF5006000850B5700A0001000A9000000060B55002A00C50B57E4
;1A0BFC000090000200E50B5700260B55008800D0FFF400600085F0010060FF
;00000001FF


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Mon Jul 02, 2012 5:41 pm 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Fri Mar 14, 2014 9:35 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Mon Mar 14, 2016 1:25 pm 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
and another. This one needs as big an audience as it can get :)


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Mon Mar 14, 2016 3:10 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Mon Mar 14, 2016 7:02 pm 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
dclxvi wrote:
Just supply an OUTPUT routine, which outputs the character in the accumulator (like pretty much every other output routine ever).

Simple:
Code:
STA  PORT

:D


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Tue Mar 15, 2016 3:34 am 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Tue Mar 15, 2016 4:19 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
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.

Aw...Now look what you did! You're going to have to re-write an awful lot of text books and programs and... (You have another 16 years to do it though.)

_________________
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Tue Mar 15, 2016 7:19 am 
Offline

Joined: Mon Jan 26, 2015 6:19 am
Posts: 85
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 don't see why anybody would have to make an issue about this. As your link says, "The traditional definition for the circle constant sets π (pi) equal to the ratio of a circle’s circumference to its diameter:".

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 :mrgreen:).


Top
 Profile  
Reply with quote  
 Post subject: Re: A program for today
PostPosted: Tue Mar 15, 2016 8:53 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10985
Location: England
I'm all for using metric over imperial, but τ is a step too far for me. Exhibit A.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 39 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: