Jump to content
IGNORED

Mad Pascal


Recommended Posts


program MonteCarloPi;

uses crt;

{$define FAST}
{$f $70}

var
    rtClock1              : byte absolute 19;
    rtClock2              : byte absolute 20;
    rndNumber             : byte absolute $D20A;

{$ifdef FAST}
    stop : word absolute $e0;
    i                     : word absolute $e0;
    r                     : word absolute $e2;
    x                     : word absolute $e4;
    y                     : word absolute $e6;
    bingo                 : word absolute $e8;
    probe                 : word absolute $ea;
    foundPi               : word absolute $ec;
    n                     : byte absolute $ee;
{$else}
    stop, i, r, x, y      : word;
    bingo, probe, foundPi : word;
    n                     : byte;
{$endif}

begin
    bingo := 0;
    r := 127 * 127;
    probe := 10000;

    Pause;    
    rtClock1 := 0; rtClock2 := 0;
    
    for i := 0 to probe do begin
        n := (rndNumber or %10000000) xor %10000000;
        x := n * n;
        n := (rndNumber or %10000000) xor %10000000;        
        y := n * n;
        if (x + y) <= r then Inc(bingo);
    end;
    
    foundPi := 4 * bingo;
    stop := (rtClock1 * 256) + rtClock2;
    
    {$ifdef FAST}
    WriteLn('Variables on zero page');
    {$endif}
    WriteLn('Probe size ', probe);
    WriteLn('Points in circle ', bingo);
    Write('Found pi approximation ', foundPi);
    WriteLn(chr(30),chr(30),chr(30),chr(30),chr(255),chr(44));
    WriteLn('Frames counter = ', stop);
    ReadKey;
end.


PROBE=10000:B=0:R=127*127:F=PEEK(20)
WHILE F=PEEK(20)
WEND
POKE 20,0:POKE 19,0
FOR I=0 TO PROBE
 N=PEEK($D20A):N=N!128:N=N EXOR 128
 X=N*N
 N=PEEK($D20A):N=N!128:N=N EXOR 128
 Y=N*N
 IF (X+Y)<=R THEN INC B
NEXT I
P=4*B
T=TIME
? P;CHR$(30);CHR$(30);CHR$(30);CHR$(30);CHR$(255);CHR$(46)
? T;" FRAMES" 

YoshPlus&Pi-Bench.png

YoshPlus&Pi - Benchmarks.ods

monte-carlo-pi-sources.zip

Edited by zbyti
sources
Link to comment
Share on other sites

#include <stdio.h>

void main(void)
{
  register unsigned int stop, i, x, y;
  register unsigned int b, p, r;
  register unsigned char n;
  register unsigned char* rndp = 0xd20a;
  register unsigned char* tick = 0x14;
  register unsigned char* tack = 0x13;

  b = 0;
  r = 127 * 127;
  p = 10000;

  n = *tick;
  while (*tick == n) { ; }

  printf("\nMonte-Carlo PI cc65 V2.18\n");

  asm(" lda #0");
  asm(" sta $13");
  asm(" sta $14");

  for (i = 0 ; i < p; ++i)
  {
    n = (*rndp | 128) ^ 128;
    x = n * n;
    n = (*rndp | 128) ^ 128;
    y = n * n;
    if ((x + y) <= r)
      ++b;
  }
  b = 4 * b;
  stop = *tick + (*tack * 256);

  printf("%d%c%c%c%c%c%c\n", b, 30, 30, 30, 30, 255, 46);
  printf("%d ticks\n", stop);

  infinite:
    goto infinite;
}
cl65 -t atari -o mcpi.xex mcpi.c           602 ticks
cl65 -t atari -Osir -Cl -o mcpi.xex mcpi.c 303 ticks
	

Screenshot_2020-03-02_00-49-02.png

YoshPlus&Pi - Benchmarks.ods

Edited by zbyti
Link to comment
Share on other sites

#include <stdio.h>
#include <peekpoke.h>

#define p 10000
#define r 16129
#define tick		(PEEK(0x14))
#define tack		(PEEK(0x13) * 256)
#define rndp 		(PEEK(0xd20a))

void main(void)
{
  register unsigned int stop, i, x, y;
  register unsigned int b = 0;
  register unsigned char n;

  n = tick;
  while (tick == n) { ; }

  printf("\nMonte-Carlo PI cc65 V2.18\n");

  asm(" lda #0");
  asm(" sta $13");
  asm(" sta $14");

  for (i = 0 ; i < p; ++i)
  {
    n = (rndp | 128) ^ 128;
    x = n * n;
    n = (rndp | 128) ^ 128;
    y = n * n;
    if ((x + y) <= r)
      ++b;
  }
  b *= 4;
  stop = tick + tack;

  printf("%d%c%c%c%c%c%c\n", b, 30, 30, 30, 30, 255, 46);
  printf("%d ticks\n", stop);

  infinite:
    goto infinite;
}

 

Screenshot_2020-03-02_10-01-43.png

Edited by zbyti
add macros
Link to comment
Share on other sites

Also, does not CC65 support such use of pointers as:

 

n = *(unsigned char *)0xd20a & 0x7f;

or (instead of the asm snippet):

*(int *)0x13 = 0;

Also, this:

 infinite:
    goto infinite;

would be better said as something like that:

 for(;;)
   ;

(although it in no way affects performance, of course).

  • Like 2
Link to comment
Share on other sites

@Wrathchild no, it's not the same.

@drac030 thx :]


#include <stdio.h>

#define p 10000
#define r 16129

#define RTCLOK1 (*(volatile unsigned char*)0x13)
#define RTCLOK2 (*(volatile unsigned char*)0x14)
#define RANDOM  (*(volatile unsigned char*)0xd20a)

void main(void)
{
  register unsigned int stop, i, x, y;
  register unsigned int b = 0;
  register unsigned char n;

  n = RTCLOK2;
  while (RTCLOK2 == n) { ; }

  printf("\nMonte-Carlo PI cc65 V2.18\n");

  RTCLOK1 = 0; RTCLOK2 = 0;
  for (i = 0 ; i < p; ++i)
  {
    n = (RANDOM | 128) ^ 128;
    x = n * n;
    n = (RANDOM | 128) ^ 128;
    y = n * n;
    if ((x + y) <= r)
      ++b;
  }
  b *= 4;
  stop = RTCLOK2 + 256 * RTCLOK1;

  printf("%u\x1e\x1e\x1e\x1e\xff.\n%u ticks\n", b, stop);

  for (;;);
}

Link to comment
Share on other sites

Yes, I see no difference either: n is unsigned char, so random | 128 ^ 128 = LDA RANDOM / ORA #$80 / EOR #$80 which effectively, when bit 7 is set, sets it again, and inverts (clears) it later; and when it is clear - first sets it and inverts (clears) later. So it is AND #$7F = &0x7F (as said already above).

Edited by drac030
typos, many typos
Link to comment
Share on other sites

Hi!

3 hours ago, zbyti said:

@Wrathchild no, it's not the same.

@drac030 thx :]

A little more "idiomatic" C :

 

#include <stdio.h>
#include <atari.h>

#pragma static-locals(on)

#define p 10000
#define r 16129

void main(void)
{
  register unsigned int x, y;
  unsigned int stop, i, b = 0;
  register unsigned char n;

  n = OS.rtclok[2];
  while (OS.rtclok[2] == n) { ; }

  printf("\nMonte-Carlo PI cc65 V2.18\n");

  OS.rtclok[1] = 0; OS.rtclok[2] = 0;
  for (i = 0 ; i < p; ++i)
  {
    n = (POKEY_READ.random | 128) ^ 128;
    x = n * n;
    n = (POKEY_READ.random | 128) ^ 128;
    y = n * n;
    if ((x + y) <= r)
      ++b;
  }
  b *= 4;
  stop = OS.rtclok[2] + 256 * OS.rtclok[1];

  printf("%u.%04u\n%u ticks\n", b/10000,b%10000, stop);

  for (;;);
}

 

Also, writing (n = POKEY_READ.random & 0x7F) would be a little faster.

 

Have Fun!

 

  • Like 1
Link to comment
Share on other sites

47 minutes ago, Wrathchild said:

@zbyti no worries

 

POKEY's random number generator should in no way be seen as a good candidate for use in a Monte Carlo simulation :D 

It's true, but SID it's no better :D

//cl65 -t c64 -Osir -Cl -o mcpi.prg mcpi-c64.c
#include <stdio.h>

#define p 10000
#define r 16129
#define RTCLOK1 (*(volatile unsigned char*)0xa1)
#define RTCLOK2 (*(volatile unsigned char*)0xa2)
#define RANDOM  (*(volatile unsigned char*)0xd41b)

void main(void)
{
  register unsigned int stop, i, x, y;
  register unsigned int b = 0;
  register unsigned char n;

//SID's Random Number Generator
  asm(" lda #$ff");     //maximum frequency value
  asm(" sta $D40E");    //voice 3 frequency low byte
  asm(" sta $D40F");    //voice 3 frequency high byte
  asm(" lda #$80");     //noise waveform, gate bit off
  asm(" sta $D412");    //voice 3 control register

  n = RTCLOK2;
  while (RTCLOK2 == n) { ; }

  printf("\nMonte-Carlo PI cc65 V2.18\n");

  RTCLOK1 = 0; RTCLOK2 = 0;
  for (i = 0 ; i < p; ++i)
  {
    n = RANDOM & 127;
    x = n * n;
    n = RANDOM & 127;
    y = n * n;
    if ((x + y) <= r)
      ++b;
  }
  b *= 4;
  stop = RTCLOK2 + 256 * RTCLOK1;

  printf("%u.%04u\n%u ticks\n", b/p,b%p, stop);

  for (;;);
}

Screenshot_2020-03-03_19-48-51.png

Edited by zbyti
Link to comment
Share on other sites

On 3/3/2020 at 8:53 AM, bocianu said:

n = (rndp | 128) ^ 128

 

When I want to turn bit 7 off I usually write it as:

n = rndp & ~(1<<7)

Turning it on is like:

n = rndp | (1<<7)

And testing it is like:

if( rndp & (1<<7) != 0 )

the != 0 is not strictly necessary.

 

Link to comment
Share on other sites

  • 2 weeks later...
42 minutes ago, tebe said:

current color palette is clear

 


 SetRGBPalette(0);                  // set index to 0

 SetRGBPalette(0,0,0);            // palette[0] = rgb(0,0,0)
 SetRGBPalette(100,100,100); // palette[1] = rgb(100,100,100)

 

test_vbxe.zip 2.01 kB · 0 downloads

Is there a way to determine which of the 4 palettes is being used?

Link to comment
Share on other sites

  • 2 weeks later...
On 2/25/2020 at 10:17 AM, zbyti said:

Here are my tests on atari800 PAL :] Sources mostly included ;)

Programming languages in the latest official version for February 20, 2020.

 

A8 SIEVE Benchmark.png

 

 

A8-Benchmarks.zip 59.57 kB · 5 downloads

For me, the most interesting thing about this was just how slow PL65 was. It was my language of choice. Still have my original disk here somewhere. I suspect that the problem was that it constantly jumped to its runtime library which, IIRC, was quite large and clearly not very well optimised.

Link to comment
Share on other sites

2 hours ago, Tickled_Pink said:

Still have my original disk here somewhere.

If you find it and know someone with the right equipment, then please have it dumped.

PL65 is not yet available as a preservation dump. I know of only one .PRO-file here on AtariAge at all.

Link to comment
Share on other sites

2 hours ago, DjayBee said:

If you find it and know someone with the right equipment, then please have it dumped.

PL65 is not yet available as a preservation dump. I know of only one .PRO-file here on AtariAge at all.

Yeah. That was probably the one I originally dumped when I had an APE interface. I'm not sure how to dump it properly into a standard XEX file.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...