Friday, July 11, 2014

Timing Advance Processor with a PIC



Timing advance processors are electronic control units that change the original spark advance of the engine when it runs on CNG or LPG, thus optimising the engine’s operation with these fuels.

Because LPG is a higher octane fuel than petrol the combustion time is longer. It is possible to change the ignition timing mechanically or electronically. This makes the difference between driving on LPG or petrol less. The fuel consumption and the engine power at low speed could be the same or even better than on petrol. Also the engine will run more smoothly and the exhaust emission values will be better.



 
;**************************************************************************
;Svetlin Spasov
;Basic Timing Advance Processor for LPG and CNG powered vehicles
;
;**************************************************************************

        LIST P=16F877, R=DEC
#include          ; processor specific variable definitions
      __config _HS_OSC & _WDT_ON & _LVP_OFF  


;--------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------


;angle           res     1  
;v_work          res     1
;back            res     1
;up              res     1

ScratchPadRam   EQU     0x20

;--------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------

angle    EQU     ScratchPadRam+0
v_work   EQU     ScratchPadRam+1
back     EQU     ScratchPadRam+2
back1    EQU     ScratchPadRam+3
up       EQU     ScratchPadRam+4
up1      EQU     ScratchPadRam+5
store    EQU     ScratchPadRam+6


;--------------------------------------------------------------------------
; Program Code
;--------------------------------------------------------------------------
;--------------------------------------------------------------------------
;   Set the reset vector here.  If you are using a PIC16C5X device, use:
;               ORG     
;   Otherwise, use:
;               ORG     0
;--------------------------------------------------------------------------

                ORG     0       
                GOTO    Start
                
                ;comp = 3
MORE_THAN macro comp ,var0 , jump
    MOVLW comp ; slagame comp v accumulatora
 SUBWF var0, 0      ; ako v starshia registyr ima po-malko ot comp, obshtoto 4islo ne moje da e po-goliamo ot 1000
 BTFSC STATUS, Z 
    GOTO jump;
endm
                ;comp = 3
LESS_THAN macro comp ,var0 , jump
    MOVLW comp ; slagame comp v accumulatora
 SUBWF var0, 0      ; ako v starshia registyr ima po-malko ot comp, obshtoto 4islo ne moje da e po-goliamo ot 1000
 BTFSS STATUS, Z 
    GOTO jump;
endm

EQ_JP_EQUAL macro comp, var0, jump
MOVF comp;
XORWF var0,0;
BTFSC STATUS,Z;
GOTO jump;
endm

EQ_JP_NEQUAL macro comp, var0, jump
MOVF comp;
XORWF var0,0;
BTFSS STATUS,Z;
GOTO jump;
endm

MORE_THAN_16BIT macro compH, compL,varH,varL,jump ; jump to address if more than

      MOVLW varH ; sloj starshia reg v akumulatora
   BCF STATUS, RP1 ; izberi banka
   BCF STATUS, RP0 ; banka
   SUBWF compH, 0 ; izvadi tova i stoinostta v akumulatora
   BTFSS STATUS, Z ; proveri statusa za 0 bit
   GOTO L_setAngle_25 ; jump kym izhoda
   MOVLW varL          ; slovi mladshia v akumulatora
   SUBWF compL, 0     ; izvadi gi sus sravniavanoto 4islo
L_setAngle_25:
      BTFSC STATUS, C   ; proveri status za polojitelen ili otricatelen rezultat ot operaciata
   GOTO jump        ; sko4i, ako resultata e polojitelen

endm
;--------------------------------------------------------------------------
; Main Program
;--------------------------------------------------------------------------

                ORG     ScratchPadRam+7

Start
CLRWDT ; ku4eto v kusheto
CLRW ; accumulatora v kire4a
MOVLW b'11111111' ; portb initializaciq
BANKSEL TRISB
MOVWF TRISB  
               ; bankata e 0

MOVLW b'00000000' ; kak pishem tuka kato e vhod...
BANKSEL PORTB
MOVWF PORTB
   


clrw
BANKSEL TRISC

   CLRF TRISC ; izhoda 0
   
  BANKSEL PORTC
   CLRF PORTC    ; nulirame izhoda
   CLRW
BANKSEL angle 
    CLRF v_work ; same bank as angle, virtualen raboten resistyr
    CLRF angle
    CLRF back ;
    CLRF back1 ;
    CLRF up ;
    CLRF up1 ;

    
    BANKSEL angle;
    MOVLW b'00000011' ; malko ugal za test
    MOVWF angle ;
    MOVF angle,0;
    MOVWF store ; store angle

      

 loop:  

    MOVLW b'00000011' ; malko ugal za test
    MOVWF angle ;

     BTFSC PORTB , 1 
     RLF angle, 1 ;   //umnojavame X 2 
     BTFSC PORTB , 2 
     RLF angle, 1 ;   //umnojavame X 2 
     BTFSC PORTB , 3 
     RLF angle, 1 ;   //umnojavame X 2 
     BTFSC PORTB , 4 
     RLF angle, 1 ;   //umnojavame X 2 


    BTFSS PORTB , 0 ; // (if portb.f0 == 0 && v_work.f0 == 1)
    BTFSS v_work , 0 ;
    GOTO chek1 ;
    BCF v_work, 0 ;
    CLRF back
    CLRF back1
    MOVF up, 0     ;zapazvame broaia4a v back
    MOVWF back
    MOVF up1,0;
    MOVWF back1; 
    GOTO setAngle;
    retsetAng:

    CLRF up ;
    CLRF up1;
 chek1;
    BTFSC PORTB,0; // if portb.f0 == 1 && v_work.f0 == 0
    BTFSC v_work,0;
    GOTO cont;
    BSF v_work,0;
    MOVF PORTB , 0
    MOVWF PORTC
    GOTO loop; 
 cont:
   BTFSC PORTB,0; // ako nivoto e visoko, uveli4i broaia4a na visoko nivo
   GOTO proc;
    

   GOTO loop
  
 proc:
   CLRWDT
   GOTO incUp;  // uveli4i broia4a
 retUp:

  ;DECFSZ back,1;
   CALL decBack;  // namali back broia4a
   ;DECFSZ back,1;
   MOVLW 0 
   XORWF back,0; ako "back" broaia4a stane 0
   BTFSS STATUS,Z
   GOTO loop;   // mladshia ne e 0 produlji
   MOVLW 0   
   XORWF back1,0
   BTFSS STATUS,Z
   GOTO loop;  //starshia ne 0, produlji

   BCF PORTC,0; // ako back stane 0 nivoto dolu





GOTO loop;

setAngle:

   
MORE_THAN_16BIT up1,up, 3 , 200 , L_setAngle_0 ; proverka na oborotite


  BANKSEL angle;
  
     MOVF up, 0     ;zapazvame broaia4a v "back"
     MOVWF back
     MOVF up1,0;
     MOVWF back1; 

     MOVF up1,0;     // sloji starshia registyr v store
     MOVWF store;    // poneje, veroiatno starshia registyr ne e uspial da se uveli4i dostatu4no pri visoki oboroti
     ;RLF store, 1 ;   //umnojavame X 2 
     ;RLF store, 1 ;   //umnojavame oshte X 2 


dec11:
     CALL decBack; 
     DECFSZ store,1;  
     GOTO dec11;       // loop

 

 GOTO retsetAng; / iz4ee !


L_setAngle_0: ; dec back counter

     MOVF angle,0;
     MOVWF store; //store ang > "store"
  
     dec: 
     ; DECF back,1;
     CALL decBack; 
     DECFSZ store,1;  
     GOTO dec;       // loop
GOTO retsetAng; // iz4ee!



incUp:
   INCF up, 1
   BTFSC STATUS, Z
   INCF up1, 1

GOTO retUp;

incBack:
   INCF back, 1
   BTFSC STATUS, Z
   INCF back1, 1

RETURN;


decBack: ;it's a func cos it's called more than 1 times, maybe it should be inlined for best speed instead of code size
            MOVLW 1
   SUBWF back, 1
   BTFSS STATUS, C
   DECF back1, 1
            
            
RETURN;

    
END


Wednesday, July 9, 2014

Z-brush staggering

A few years ago, the revolution in the form of Z-Brush-like programs was just gaining momentum. My first contact with a similar approach to 3D modeling was naturally with Z-Brush itself. Pixologic were the big boys in the field back then.
I liked the idea, the workflow felt much more natural and intuitive than the classical method of moving vertices and splitting edges.

This is one of my first attempts to do something useful with it. Unfortunately by the time I had a very slow computer, so it looks a bit like stop motion animation with a clay figure.


Then I made a very simple program that tried to mimic some of the basic Z-brush functionality. It was written in Borland C++ Builder 6.0 and uses DirectX 9.0 for visualization. I still have it somewhere on my hard drive and most probably will post or link the code here if someone is interested.




Friday, June 20, 2014

A small collection of images from the past

I decided to make a post and collect some old videos and screenshots from the game I'm working on.


An attempt to port the engine to OpenGL












                                             




Monday, June 16, 2014

Let there be Light-shafts !

Light is all around us. Our brains are CPU's,  processing mostly light signals. Simulating some lighting effects in 3D applications is not a trivial task. Fortunately, some effects can be simulated dynamically with relatively small computing power with a pretty satisfactory result.
As I already have all the necessary ingredients as a side effect of possessing a deferred renderer, I tried to used them to make a simple light-shaft effect. This lighting effect appears when light travels trough a foggy, dusty, full of small particles medium like air, water or pure industrial smog. The shadow of objects blocking light become visible not only on shaded surfaces, but also on the medium. These are so called light-shafts, God Rays or whatever you like to call them.



Several approaches to achieve this effect exists in real-time applications and games. Some of them render bright parts of the scene into a small render target, then blur the resulting bright pixels towards the light 2D position and blend the result with the final frame buffer.
Having access to the scene depth buffer and light depth buffer, there is a simple technique that could be used to achieve similar results without presenting the limitations that the above technique suffers.

Trace a ray, starting from eye position in world space toward the scene for every pixel on the scene. This could be optimized greatly by downscaling, bluring and upscaling.
Advance the ray position by a small step and for every step, check to see if that world position can see the light. If it can see it, make it a bit brighter for every step, thus accumulating light and make it appear as small airborne particles reflect more and more light toward the observers eye. For the most part, there are 2 precision aspects you may be concerned. The one is the precision you sample the scene -i.e. do you need to sample the scene for every pixel or "sparse" it a bit, or even do it on a downscaled target. And the second one is how big are your steps toward the scene, and haw many samples you take per ray. In the example bellow I make 64 steps per ray.

Warning HLSL ahead, but the code is pretty simple and it doesn't use advance modern features, so it should be doable in every old version of most shading languages.
 
   float3 currentTestWorldPos = eyePos ; //start with eye's position
   float3 currentRayDir = normalize(vWorldPos.xyz - eyePos) * 2.0f ; //scale it a bit. This can step 2 units further //for every step, from eye position to current pixel position in world space.
 

  for(int i = 0 ; i < 64 ; i++ )
  {
        currentTestWorldPos += currentRayDir ; //advance further

  float3 currentLightDirection = currentTestWorldPos - c_vLightPos.xyz  ; // optain direction to light source for every //test position
  float currentDistance = length(currentLightDirection.xyz);
  currentLightDirection.xyz = currentLightDirection.xyz / currentDistance; //normalize

   //sample depth from cubic shadow map                           
     float currentshadowMapDepth = texCUBE(cubeShadowMapSampler, float4((currentLightDirection.xyz), 0.0f)).x;
  //depth comparison
  if(currentDistance  < (currentshadowMapDepth + 1 ))  
  {
    color *= 1.07f; //make it a bit brighter
  }
 
           //not really necessary, but could be useful in some situations to add contrast.
           else { color *= 0.98f; }//make it a bit darker
  }

Here is how it looks statically and dynamically





Friday, February 28, 2014

The importance of global illumination

Every modern graphics engine needs a global illumination solution. Rage AFAIK uses some kind of path tracing + some static lighting baked into textures(kinda like the good old lightmapping) as they have "inlimited" amount of texture memory. Unreal and other big boys, as DICE use Enlighten, CryTek have their Light Propagation Volumes.
My solution isn't  that advanced, but can be tweaked to look OK. The main drawback of it is that the lighting is static.


Tuesday, February 25, 2014

3ds max - Office chair speed modeling.

I continue to play with 3ds max.
An attempt to model an office chair.



And here is the ingame test of the textured model.


Monday, February 24, 2014

Learning to crawl with 3ds max

For the needs of some of my hobby projects I often have a need for specific assets and level geometry.