Old school 3-D Vector Gaphics on new system

Topics relating to PALs, CPLDs, FPGAs, and other PLDs used for the support or creation of 65-family processors, both hardware and HDL.
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

I am fascinated by computer graphic algorithms based on integers. Forgive the self-indulgence, this will be a pic/code/video heavy thread based on the mild success of the Parallel Video Board system that is outputting 1920x1080 video @60Hz with 65K colors. It is highly programmable and it's not finished yet but it is outputting some neat stuff which I'd like to show off here.

Did I mention I like math? Specifically geometry and less so trigonometry. I had a great 6th grade geometry teacher. Things clicked in my brain back then which I remember to this day. Also things I remember from plotting graphics in Atari 800 BASIC over 20 years ago which was maybe 2-3 years after the 6th grade. Also, vector graphic games like Star Wars were a huge inspiration.

Most of the stuff I post is meant for inspiration. Sometimes I know it is too much detail oriented, especially the code. But if it inspires one person then that's a good thing.

The PVB currently has 2 16Kx11 blockRAMs which are meant for X and Y plot scratchpad information for the cpu which is a 65Org16.b. It's basically a 16-bit 6502 with some bells and whistles.

There is also a 10-bit hardware sine LUT present inside the FPGA. The cpu outputs a phase number from 0 to 1023 into that module, and awaiting on the cpu bus is the sinewave data.

The reason for 11-bits in the blockRAMs and 10-bit sine LUT, is so that one can add offsets to the sine LUT data and just plot the values from the scratchpad BRAMs within the 1920x1080 grid. I had wrestled with making the scratchpad BRAMs only 10-bits wide, but the software got messy trying to place waveforms with offsets.
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

So first thing was to know exactly what the sinewave LUT was outputting. This LUT is a module created by the Xilinx Core Generator tool in ISE14.1, I chose the DDS compiler. It is very conservative as far as resources used.
Attachments
set LUT for 1K width & depth
set LUT for 1K width & depth
set for sine only, full amplitude
set for sine only, full amplitude
set for 1 cycle lookup
set for 1 cycle lookup
final settings confirmation
final settings confirmation
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

After some tweaks to the LUT interface, I was able to get good data. I had half sines in different portions of the screen before this code. Basically it adjusts the Y values for the 1080 vertical:

Code: Select all

`timescale 1ns / 1ps
module sin_IF ( input clk,
					input [31:0] cpuAB,
					input [15:0] cpuDO,
					output reg [15:0] cpuDI,
					input cpuWE,
					input phaseEN,									//phaseEn = 1 when 32'hB000_0000 - 32'hBFFF_FFFF is on cpu address bus
					output reg [SINaw-1:0] phase = 0,
					input [SINaw-1:0] sin_in
					);

parameter SINaw = 10;

always @(posedge clk) begin										//write to phase register
	if (cpuWE && phaseEN && cpuAB [1:0] == 2'b00)
		phase <= cpuDO;
	
	if (!cpuWE && phaseEN && phase > 512)						//read from sine LUT
		cpuDI <= sin_in - 512;
	if (!cpuWE && phaseEN && phase <= 512)
		cpuDI <= sin_in + 512;
	
		else if (!phaseEN) 
			cpuDI <= 16'h0000;
end

endmodule
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

Now I remembered from days past that to plot a circle in an X/Y system using sine/cosine values all one had to do is put sine values in the X and cosine values in the Y. Since cosine is sin shifted by 1/4 full cycle timewise, we don't really need cosine in this application. The X value is sine the Y value is sine+1/4=cosine(256/1024)
The code below illustrates this idea. In addition, it adds vertical and horizontal offsets through XORG and YORG for easy plotting. This is why I decided to keep the scratchpad BRAMs at 11-bits for 1920x1080 resolution. Also it divides the data from the LUT by 4 by performing 2x LSR. This code only takes data from the LUT, the cpu modifies it and then places it in BRAM:

Code: Select all

;---------  Circle/Ellipse Generator from sine wave LUT
                  LDA #750
                  STA XORG
                  LDA #350
                  STA YORG
                  
                  LDWi $0000             
                  LDX #0
                  LDY #256
sinwave1          STX phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC XORG
                  STAaw scratchx1        
                  STY phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC YORG
                  STAaw scratchy1        
                  CPX #1023
                  BMI XNZ1
                  LDX #0
XNZ1              INX                   
                  CPY #1023
                  BMI YNZ1
                  LDY #0
YNZ1              INY
                  INW
                  CPWi $03FF             
                  BNE sinwave1
This simple code takes the data from the scratch BRAM and plots it. It's got to be simple, or I won't tolerate it.
ONE IMPORTANT NOTE:
When Y=0, a dot will be placed at 6o'clock.
When Y=256, a dot will be placed at 3o'clock.
When Y=512, a dot will be placed at 12o'clock.
When Y=768, a dot will be placed at 9o'clock.

Code: Select all

                  LDX #1            ;white
                  LDA CLUT,X
                  STA color
                 
                  LDY #0
plot1             LDA scratchx1,Y
                  STA Xp
                  LDA scratchy1,Y
                  STA Yp
                  INY
                  CPY #1023
                  BMI plot1
Attachments
circle from 1024-point LUT.JPG
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

I'm going for a 3-D rotating cube with some special effects. :wink:

Software for first square face of cube inside the circle:

Code: Select all

triplot           LDX #5            ;green
                  LDA CLUT,X
                  STA color
                  
                  BCF1C $FFFE              ;Branch if Control Flag 1 is Clear (0), CF1 is VSYNC input, so branch to itself and wait until vsync = 1, a non display period

                  LDY #128                 ;1st SQUARE OF CUBE
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #512+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #768+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
Attachments
1st face.JPG
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

Now 2 scratchpads for 2 circles offset for a cube structure, I tried to come up with a quick calculation for the unit length. It didn't work at first so I placed a set value for UNITL:

Code: Select all

;---------  Circle/Ellipse Generator from sine wave LUT
                  LDA #750
                  STA XORG
                  LDA #350
                  STA YORG
                  
                  LDWi $0000             
                  LDX #0
                  LDY #256
sinwave1          STX phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC XORG
                  STAaw scratchx1        
                  STY phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC YORG
                  STAaw scratchy1        
                  CPX #1023
                  BMI XNZ1
                  LDX #0
XNZ1              INX                   
                  CPY #1023
                  BMI YNZ1
                  LDY #0
YNZ1              INY
                  INW
                  CPWi $03FF             
                  BNE sinwave1
                  
                  ;LDY #128 + 768                   ;unit length calculation
                  ;LDA scratchy1,Y
                  ;LDY #128
                  ;SEC
                  ;SBC scratchy1,Y
                  ;LSR
                  LDA #85
                  STA UNITL                  
                  
                  LDWi $0000             
                  LDX #0
                  LDY #256
sinwave2          STX phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC XORG
                  ADC UNITL
                  STAaw scratchx2        
                  STY phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC YORG
                  ADC UNITL
                  STAaw scratchy2        
                  CPX #1023
                  BMI XNZ2
                  LDX #0
XNZ2              INX                   
                  CPY #1023
                  BMI YNZ2
                  LDY #0
YNZ2              INY
                  INW
                  CPWi $03FF             
                  BNE sinwave2
That was for the circles, which are virtex modifiers. Now 2 squares set inside 2 circles.
KEEP IN MIND EVERY TIME THE HARDWARE LINE GENERATOR SEES THE ADDRESS ly1, regardless of read or write, IT AUTO DRAWS from (lx0,ly0) to (lx1,ly1):

Code: Select all

triplot           LDX #5            ;green
                  LDA CLUT,X
                  STA color
                  
                  BCF1C $FFFE              ;Branch if Control Flag 1 is Clear (0), CF1 is VSYNC input, so branch to itself and wait until vsync = 1, a non display period

                  LDY #128                 ;1st SQUARE OF CUBE
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #512+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #768+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
                  
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1                 
                                            ;2nd SQUARE OF CUBE
                  LDY #128
                  LDA scratchx2,Y
                  STA lx0
                  LDA scratchy2,Y
                  STA ly0
                  
                  LDY #256+128
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDA scratchx2,Y
                  STA lx0
                  LDA scratchy2,Y
                  STA ly0
                  
                  LDY #512+128
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDA scratchx2,Y
                  STA lx0
                  LDA scratchy2,Y
                  STA ly0
                  
                  LDY #768+128
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDA scratchx2,Y
                  STA lx0
                  LDA scratchy2,Y
                  STA ly0
                  
                  LDY #128
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
Attachments
2nd face.JPG
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

Now to add the 4 diagonal interconnects:

Code: Select all

                                          ;DIAGONAL INTERCONNECTS
                  LDY #128                 
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDY #512+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                  
                  LDY #768+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
Attachments
3D Cube.JPG
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Old school 3-D Vector Gaphics on new system

Post by GARTHWILSON »

Time for macros! :D They would dramatically shorten the source code and make it more clear, and still assemble the same result.
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?
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

So now, it would be neat to rotate one of the squares for an cool effect soon, but first...
I shift the Y index for the X sine on both of the LUTs represented as the circles currently, in order to make an ellipse. When X=0, Y=256 you get a circle. When X=812(arbitrary), Y=256 you get ellipse...

Code: Select all

;---------  Circle/Ellipse Generator from sine wave LUT
                  LDA #750
                  STA XORG
                  LDA #350
                  STA YORG
                  
                  LDWi $0000             
                  LDX #812
                  LDY #256
sinwave1          STX phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC XORG
                  STAaw scratchx1        
                  STY phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC YORG
                  STAaw scratchy1        
                  CPX #1023
                  BMI XNZ1
                  LDX #0
XNZ1              INX                   
                  CPY #1023
                  BMI YNZ1
                  LDY #0
YNZ1              INY
                  INW
                  CPWi $03FF             
                  BNE sinwave1
                  
                  ;LDY #128 + 768                   ;unit length calculation
                  ;LDA scratchy1,Y
                  ;LDY #128
                  ;SEC
                  ;SBC scratchy1,Y
                  ;LSR
                  LDA #85
                  STA UNITL                  
                  
                  LDWi $0000             
                  LDX #812
                  LDY #256
sinwave2          STX phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC XORG
                  ADC UNITL
                  STAaw scratchx2        
                  STY phase
                  LDA sinout
                  LSR
                  LSR
                  CLC
                  ADC YORG
                  ADC UNITL
                  STAaw scratchy2        
                  CPX #1023
                  BMI XNZ2
                  LDX #0
XNZ2              INX                   
                  CPY #1023
                  BMI YNZ2
                  LDY #0
YNZ2              INY
                  INW
                  CPWi $03FF             
                  BNE sinwave2
Attachments
3D cube mod.JPG
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

GARTHWILSON wrote:
Time for macros! :D They would dramatically shorten the source code and make it more clear, and still assemble the same result.
Great, give us an example Garth!
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Old school 3-D Vector Gaphics on new system

Post by GARTHWILSON »

ElEctric_EyE wrote:
GARTHWILSON wrote:
Time for macros! :D They would dramatically shorten the source code and make it more clear, and still assemble the same result.
Great, give us an example Garth!
Actually, in the portion

Code: Select all

                  LDY #128                 
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                 
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                 
                  LDY #512+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
                 
                  LDY #768+128
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
there's more similarity that I saw at first glance, and the

Code: Select all

                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  LDA scratchx2,Y
                  STA lx1
                  LDA scratchy2,Y
                  STA ly1
portion could even be a subroutine, unless you don't want the JSR/RTS overhead, in which case you could make it a macro and include the LDY# line, like

Code: Select all

FOOBAR: MACRO  input        ; (Give it a descriptive name though.)
        LDY  #input
        LDA  scratchx1,Y
        STA  lx0
        LDA  scratchy1,Y
        STA  ly0
        LDA  scratchx2,Y
        STA  lx1
        LDA  scratchy2,Y
        STA  ly1
        ENDM
 ;------------------
and then replace the entire first section above with

Code: Select all

        FOOBAR  128
        FOOBAR  256+128
        FOOBAR  512+128
        FOOBAR  768+128
taking it from 39 lines down to four.

In applications that have a lot of LDA#...STA_abs, I like to use a macro I call PUT, like this:

Code: Select all

        PUT  750, IN, XORG
        PUT  350, IN, YORG
(Macros require commas separating the input parameters.) The "IN" is previously defined as 0 and is not actually used in the macro, being there only to make it more English-like.

When there are a lot of LDA_abs, STA_abs, I like to use a macro I call COPY, like this:

Code: Select all

        COPY  FOOBAR, TO, XLERB
(Again, "TO" is only there to make it more English-like, and is not actually used by the macro.) You could go further and add another parameter telling how many address units to copy, so if you had a 32- or 64-bit quantity to copy, it would all get handled in one line.

You also have a lot of occurrences of LSR LSR (two lines), so it would be worth making those a macro and call it LSR2 for example, and then it only uses one line each time instead of two.

The macros for looping, conditional branching, etc, giving it all a more-visible structure are extra, and now I'm hooked on them and never want to go back.
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?
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

GARTHWILSON wrote:

Code: Select all

FOOBAR: MACRO  input        ; (Give it a descriptive name though.)
        LDY  #input
        LDA  scratchx1,Y
        STA  lx0
        LDA  scratchy1,Y
        STA  ly0
        LDA  scratchx2,Y
        STA  lx1
        LDA  scratchy2,Y
        STA  ly1
        ENDM
 ;------------------
and then replace the entire first section above with

Code: Select all

        FOOBAR  128
        FOOBAR  256+128
        FOOBAR  512+128
        FOOBAR  768+128
taking it from 39 lines down to four..
Sweet! I admit, this is nice from a programmer's POV.
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

I should mention, I use macros. Currently about 16,000 lines of them, but they are only to define some of the new 65Org16.b opcodes, not to crunch software.
Like above when I use the 'W' register:

Code: Select all

STAaw scratchy1
the macro is:

Code: Select all

STAaw             .MACRO param          ;STA $xxxxxxxx,w
                  .BYTE $009B
		            .WORD  param
                  .ENDM
My focus is more on making the machine work at this point, but what you have shown I like and I'll incorporate into future software posts.
ElEctric_EyE
Posts: 3260
Joined: 02 Mar 2009
Location: OH, USA

Re: Old school 3-D Vector Gaphics on new system

Post by ElEctric_EyE »

And what about when one needs to pass potentially 2 variables like in this situation:

Code: Select all

                  LDY #128                 ;1st LINE of SQUARE OF CUBE
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
It would be tolerable for X, Y, or W values to be used for the different Y values, but what is the syntax? Also, where are the syntax rules specifying these things I ask about?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Old school 3-D Vector Gaphics on new system

Post by GARTHWILSON »

Code: Select all

                  LDY #128                 ;1st LINE of SQUARE OF CUBE
                  LDA scratchx1,Y
                  STA lx0
                  LDA scratchy1,Y
                  STA ly0
                  
                  LDY #256+128
                  LDA scratchx1,Y
                  STA lx1
                  LDA scratchy1,Y
                  STA ly1
If the input parameters are for the first and seventh lines, you could do something like

Code: Select all

FOOBAR:  MACRO first_param, second_param  ; (Name them something meaningful if you like.)
         LDY  #first_param                ; 1st LINE of SQUARE OF CUBE
         LDA  scratchx1,Y
         STA  lx0
         LDA  scratchy1,Y
         STA  ly0
                  
         LDY  #second_param
         LDA  scratchx1,Y
         STA  lx1
         LDA  scratchy1,Y
         STA  ly1

         ENDM
 ;------------------
then invoke it with, in the case you gave,

Code: Select all

         FOOBAR  128, 256+128
The three major assemblers I've used, from different suppliers, all do macros more or less the same, with only minor variations. They of course allow conditional assembly, plus most, or all, of the things you can do in non-macro situations. They don't all do everything I would like, but I've still been able to do most of what I want to. The macro is kind of a program for the assembler to run in order to form the code that the conditions dictate, and a macro may generate differing amounts and types of code depending on input parameters and other assembler EQUates and variables; so macro usage can actually be quite flexible. Then of course the benefit is that once you work out the internal details, you don't have to keep looking at them every time you use them, so you can focus better attention on where you're going with the project rather than with the mundane stuff.

I've been following your video posts with interest. The reason I have not commented much is because I don't know enough about video or HDL to comment. :lol:
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?
Post Reply